~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/definition/table.cc

  • Committer: Brian Aker
  • Date: 2010-11-06 05:47:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1909.
  • Revision ID: brian@tangent.org-20101106054712-jwxd8e0s0s3nm7qn
Merge in encapsulations in filesort.

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
25
25
*/
26
26
 
27
27
/* Basic functions needed by many modules */
28
 
#include <config.h>
 
28
#include "config.h"
29
29
 
30
30
#include <pthread.h>
31
31
#include <float.h>
37
37
 
38
38
#include <cassert>
39
39
 
40
 
#include <drizzled/error.h>
41
 
#include <drizzled/gettext.h>
42
 
#include <drizzled/sql_base.h>
43
 
#include <drizzled/pthread_globals.h>
44
 
#include <drizzled/internal/my_pthread.h>
45
 
 
46
 
#include <drizzled/table.h>
47
 
#include <drizzled/table/shell.h>
48
 
 
49
 
#include <drizzled/session.h>
50
 
 
51
 
#include <drizzled/charset.h>
52
 
#include <drizzled/internal/m_string.h>
53
 
#include <drizzled/internal/my_sys.h>
54
 
 
55
 
#include <drizzled/item/string.h>
56
 
#include <drizzled/item/int.h>
57
 
#include <drizzled/item/decimal.h>
58
 
#include <drizzled/item/float.h>
59
 
#include <drizzled/item/null.h>
60
 
#include <drizzled/temporal.h>
61
 
 
62
 
#include <drizzled/field.h>
63
 
#include <drizzled/field/str.h>
64
 
#include <drizzled/field/num.h>
65
 
#include <drizzled/field/blob.h>
66
 
#include <drizzled/field/boolean.h>
67
 
#include <drizzled/field/enum.h>
68
 
#include <drizzled/field/null.h>
69
 
#include <drizzled/field/date.h>
70
 
#include <drizzled/field/decimal.h>
71
 
#include <drizzled/field/real.h>
72
 
#include <drizzled/field/double.h>
73
 
#include <drizzled/field/int32.h>
74
 
#include <drizzled/field/int64.h>
75
 
#include <drizzled/field/size.h>
76
 
#include <drizzled/field/num.h>
77
 
#include <drizzled/field/time.h>
78
 
#include <drizzled/field/epoch.h>
79
 
#include <drizzled/field/datetime.h>
80
 
#include <drizzled/field/microtime.h>
81
 
#include <drizzled/field/varstring.h>
82
 
#include <drizzled/field/uuid.h>
83
 
 
84
 
#include <drizzled/plugin/storage_engine.h>
85
 
 
86
 
#include <drizzled/definition/cache.h>
87
 
#include <drizzled/typelib.h>
88
 
 
89
 
#include <drizzled/refresh_version.h>
 
40
#include "drizzled/error.h"
 
41
#include "drizzled/gettext.h"
 
42
#include "drizzled/sql_base.h"
 
43
#include "drizzled/pthread_globals.h"
 
44
#include "drizzled/internal/my_pthread.h"
 
45
#include "drizzled/plugin/event_observer.h"
 
46
 
 
47
#include "drizzled/table.h"
 
48
#include "drizzled/table/shell.h"
 
49
 
 
50
#include "drizzled/session.h"
 
51
 
 
52
#include "drizzled/charset.h"
 
53
#include "drizzled/internal/m_string.h"
 
54
#include "drizzled/internal/my_sys.h"
 
55
 
 
56
#include "drizzled/item/string.h"
 
57
#include "drizzled/item/int.h"
 
58
#include "drizzled/item/decimal.h"
 
59
#include "drizzled/item/float.h"
 
60
#include "drizzled/item/null.h"
 
61
#include "drizzled/temporal.h"
 
62
 
 
63
#include "drizzled/field.h"
 
64
#include "drizzled/field/str.h"
 
65
#include "drizzled/field/num.h"
 
66
#include "drizzled/field/blob.h"
 
67
#include "drizzled/field/enum.h"
 
68
#include "drizzled/field/null.h"
 
69
#include "drizzled/field/date.h"
 
70
#include "drizzled/field/decimal.h"
 
71
#include "drizzled/field/real.h"
 
72
#include "drizzled/field/double.h"
 
73
#include "drizzled/field/long.h"
 
74
#include "drizzled/field/int64_t.h"
 
75
#include "drizzled/field/num.h"
 
76
#include "drizzled/field/timestamp.h"
 
77
#include "drizzled/field/datetime.h"
 
78
#include "drizzled/field/varstring.h"
90
79
 
91
80
using namespace std;
92
81
 
95
84
 
96
85
extern size_t table_def_size;
97
86
 
98
 
 
99
 
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
100
 
{
101
 
  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)
102
273
  {
103
274
  case message::Table::Field::INTEGER:
104
 
    return DRIZZLE_TYPE_LONG;
105
 
 
 
275
    field_type= DRIZZLE_TYPE_LONG;
 
276
    break;
106
277
  case message::Table::Field::DOUBLE:
107
 
    return DRIZZLE_TYPE_DOUBLE;
108
 
 
109
 
  case message::Table::Field::EPOCH:
110
 
    if (field.has_time_options() and field.time_options().microseconds())
111
 
      return DRIZZLE_TYPE_MICROTIME;
112
 
 
113
 
    return DRIZZLE_TYPE_TIMESTAMP;
114
 
 
 
278
    field_type= DRIZZLE_TYPE_DOUBLE;
 
279
    break;
 
280
  case message::Table::Field::TIMESTAMP:
 
281
    field_type= DRIZZLE_TYPE_TIMESTAMP;
 
282
    break;
115
283
  case message::Table::Field::BIGINT:
116
 
    return DRIZZLE_TYPE_LONGLONG;
117
 
 
 
284
    field_type= DRIZZLE_TYPE_LONGLONG;
 
285
    break;
118
286
  case message::Table::Field::DATETIME:
119
 
    return DRIZZLE_TYPE_DATETIME;
120
 
 
 
287
    field_type= DRIZZLE_TYPE_DATETIME;
 
288
    break;
121
289
  case message::Table::Field::DATE:
122
 
    return DRIZZLE_TYPE_DATE;
123
 
 
 
290
    field_type= DRIZZLE_TYPE_DATE;
 
291
    break;
124
292
  case message::Table::Field::VARCHAR:
125
 
    return DRIZZLE_TYPE_VARCHAR;
126
 
 
 
293
    field_type= DRIZZLE_TYPE_VARCHAR;
 
294
    break;
127
295
  case message::Table::Field::DECIMAL:
128
 
    return DRIZZLE_TYPE_DECIMAL;
129
 
 
 
296
    field_type= DRIZZLE_TYPE_DECIMAL;
 
297
    break;
130
298
  case message::Table::Field::ENUM:
131
 
    return DRIZZLE_TYPE_ENUM;
132
 
 
 
299
    field_type= DRIZZLE_TYPE_ENUM;
 
300
    break;
133
301
  case message::Table::Field::BLOB:
134
 
    return DRIZZLE_TYPE_BLOB;
135
 
 
136
 
  case message::Table::Field::UUID:
137
 
    return  DRIZZLE_TYPE_UUID;
138
 
 
139
 
  case message::Table::Field::BOOLEAN:
140
 
    return DRIZZLE_TYPE_BOOLEAN;
141
 
 
142
 
  case message::Table::Field::TIME:
143
 
    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);
144
307
  }
145
308
 
146
 
  abort();
 
309
  return field_type;
147
310
}
148
311
 
149
312
static Item *default_value_item(enum_field_types field_type,
174
337
                                 default_value->length());
175
338
    break;
176
339
  case DRIZZLE_TYPE_NULL:
177
 
    assert(0);
178
 
    abort();
 
340
    assert(false);
179
341
  case DRIZZLE_TYPE_TIMESTAMP:
180
342
  case DRIZZLE_TYPE_DATETIME:
181
 
  case DRIZZLE_TYPE_TIME:
182
343
  case DRIZZLE_TYPE_DATE:
183
344
  case DRIZZLE_TYPE_ENUM:
184
 
  case DRIZZLE_TYPE_UUID:
185
 
  case DRIZZLE_TYPE_MICROTIME:
186
 
  case DRIZZLE_TYPE_BOOLEAN:
187
345
    default_item= new Item_string(default_value->c_str(),
188
346
                                  default_value->length(),
189
347
                                  system_charset_info);
222
380
 */
223
381
bool TableShare::fieldInPrimaryKey(Field *in_field) const
224
382
{
225
 
  assert(getTableMessage());
 
383
  assert(table_proto != NULL);
226
384
 
227
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
385
  size_t num_indexes= table_proto->indexes_size();
228
386
 
229
387
  for (size_t x= 0; x < num_indexes; ++x)
230
388
  {
231
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
389
    const message::Table::Index &index= table_proto->indexes(x);
232
390
    if (index.is_primary())
233
391
    {
234
392
      size_t num_parts= index.index_part_size();
235
393
      for (size_t y= 0; y < num_parts; ++y)
236
394
      {
237
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
395
        if (index.index_part(y).fieldnr() == in_field->field_index)
238
396
          return true;
239
397
      }
240
398
    }
242
400
  return false;
243
401
}
244
402
 
245
 
TableShare::TableShare(const identifier::Table::Type type_arg) :
 
403
TableShare::TableShare(TableIdentifier::Type type_arg) :
246
404
  table_category(TABLE_UNKNOWN_CATEGORY),
247
405
  found_next_number_field(NULL),
248
406
  timestamp_field(NULL),
249
407
  key_info(NULL),
250
408
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
251
409
  all_set(),
252
 
  db(NULL_LEX_STRING),
253
 
  table_name(NULL_LEX_STRING),
254
 
  path(NULL_LEX_STRING),
255
 
  normalized_path(NULL_LEX_STRING),
256
410
  block_size(0),
257
411
  version(0),
258
412
  timestamp_offset(0),
259
413
  reclength(0),
260
414
  stored_rec_length(0),
261
415
  max_rows(0),
262
 
  _table_message(NULL),
 
416
  table_proto(NULL),
263
417
  storage_engine(NULL),
264
418
  tmp_table(type_arg),
265
 
  _ref_count(0),
 
419
  ref_count(0),
266
420
  null_bytes(0),
267
421
  last_null_bit_pos(0),
268
 
  _field_size(0),
 
422
  fields(0),
269
423
  rec_buff_length(0),
270
424
  keys(0),
271
425
  key_parts(0),
275
429
  uniques(0),
276
430
  null_fields(0),
277
431
  blob_fields(0),
 
432
  timestamp_field_offset(0),
278
433
  has_variable_width(false),
279
434
  db_create_options(0),
280
435
  db_options_in_use(0),
287
442
  error(0),
288
443
  open_errno(0),
289
444
  errarg(0),
290
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
445
  blob_ptr_size(0),
291
446
  db_low_byte_first(false),
 
447
  name_lock(false),
 
448
  replace_with_name_lock(false),
 
449
  waiting_on_cond(false),
292
450
  keys_in_use(0),
293
 
  keys_for_keyread(0)
 
451
  keys_for_keyread(0),
 
452
  event_observers(NULL)
294
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
 
295
461
  if (type_arg == message::Table::INTERNAL)
296
462
  {
297
 
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
463
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
298
464
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
299
465
  }
300
466
  else
303
469
  }
304
470
}
305
471
 
306
 
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
307
473
  table_category(TABLE_UNKNOWN_CATEGORY),
308
474
  found_next_number_field(NULL),
309
475
  timestamp_field(NULL),
310
476
  key_info(NULL),
311
477
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
312
 
  table_charset(0),
313
478
  all_set(),
314
 
  db(NULL_LEX_STRING),
315
 
  table_name(NULL_LEX_STRING),
316
 
  path(NULL_LEX_STRING),
317
 
  normalized_path(NULL_LEX_STRING),
318
479
  block_size(0),
319
480
  version(0),
320
481
  timestamp_offset(0),
321
482
  reclength(0),
322
483
  stored_rec_length(0),
323
484
  max_rows(0),
324
 
  _table_message(NULL),
 
485
  table_proto(NULL),
325
486
  storage_engine(NULL),
326
487
  tmp_table(message::Table::INTERNAL),
327
 
  _ref_count(0),
 
488
  ref_count(0),
328
489
  null_bytes(0),
329
490
  last_null_bit_pos(0),
330
 
  _field_size(0),
 
491
  fields(0),
331
492
  rec_buff_length(0),
332
493
  keys(0),
333
494
  key_parts(0),
337
498
  uniques(0),
338
499
  null_fields(0),
339
500
  blob_fields(0),
 
501
  timestamp_field_offset(0),
340
502
  has_variable_width(false),
341
503
  db_create_options(0),
342
504
  db_options_in_use(0),
349
511
  error(0),
350
512
  open_errno(0),
351
513
  errarg(0),
352
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
514
  blob_ptr_size(0),
353
515
  db_low_byte_first(false),
 
516
  name_lock(false),
 
517
  replace_with_name_lock(false),
 
518
  waiting_on_cond(false),
354
519
  keys_in_use(0),
355
 
  keys_for_keyread(0)
 
520
  keys_for_keyread(0),
 
521
  event_observers(NULL)
356
522
{
357
523
  assert(identifier.getKey() == key);
358
524
 
 
525
  table_charset= 0;
 
526
  memset(&path, 0, sizeof(LEX_STRING));
 
527
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
528
 
359
529
  private_key_for_cache= key;
360
530
 
361
531
  table_category=         TABLE_CATEGORY_TEMPORARY;
369
539
  path.str= (char *)"";
370
540
  normalized_path.str= path.str;
371
541
  path.length= normalized_path.length= 0;
372
 
 
373
 
  std::string tb_name(identifier.getTableName());
374
 
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
375
 
  assert(strcmp(tb_name.c_str(), table_name.str) == 0);
376
 
 
 
542
  assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
377
543
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
378
544
}
379
545
 
380
546
 
381
 
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
 
547
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
382
548
  table_category(TABLE_UNKNOWN_CATEGORY),
383
549
  found_next_number_field(NULL),
384
550
  timestamp_field(NULL),
385
551
  key_info(NULL),
386
552
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
387
 
  table_charset(0),
388
553
  all_set(),
389
 
  db(NULL_LEX_STRING),
390
 
  table_name(NULL_LEX_STRING),
391
 
  path(NULL_LEX_STRING),
392
 
  normalized_path(NULL_LEX_STRING),
393
554
  block_size(0),
394
555
  version(0),
395
556
  timestamp_offset(0),
396
557
  reclength(0),
397
558
  stored_rec_length(0),
398
559
  max_rows(0),
399
 
  _table_message(NULL),
 
560
  table_proto(NULL),
400
561
  storage_engine(NULL),
401
562
  tmp_table(identifier.getType()),
402
 
  _ref_count(0),
 
563
  ref_count(0),
403
564
  null_bytes(0),
404
565
  last_null_bit_pos(0),
405
 
  _field_size(0),
 
566
  fields(0),
406
567
  rec_buff_length(0),
407
568
  keys(0),
408
569
  key_parts(0),
412
573
  uniques(0),
413
574
  null_fields(0),
414
575
  blob_fields(0),
 
576
  timestamp_field_offset(0),
415
577
  has_variable_width(false),
416
578
  db_create_options(0),
417
579
  db_options_in_use(0),
424
586
  error(0),
425
587
  open_errno(0),
426
588
  errarg(0),
427
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
589
  blob_ptr_size(0),
428
590
  db_low_byte_first(false),
 
591
  name_lock(false),
 
592
  replace_with_name_lock(false),
 
593
  waiting_on_cond(false),
429
594
  keys_in_use(0),
430
 
  keys_for_keyread(0)
 
595
  keys_for_keyread(0),
 
596
  event_observers(NULL)
431
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
 
432
604
  private_key_for_cache= identifier.getKey();
433
605
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
434
606
  private_normalized_path.resize(identifier.getPath().size() + 1);
451
623
/*
452
624
  Used for shares that will go into the cache.
453
625
*/
454
 
TableShare::TableShare(const identifier::Table::Type type_arg,
455
 
                       const identifier::Table &identifier,
 
626
TableShare::TableShare(TableIdentifier::Type type_arg,
 
627
                       TableIdentifier &identifier,
456
628
                       char *path_arg,
457
629
                       uint32_t path_length_arg) :
458
630
  table_category(TABLE_UNKNOWN_CATEGORY),
460
632
  timestamp_field(NULL),
461
633
  key_info(NULL),
462
634
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
463
 
  table_charset(0),
464
635
  all_set(),
465
 
  db(NULL_LEX_STRING),
466
 
  table_name(NULL_LEX_STRING),
467
 
  path(NULL_LEX_STRING),
468
 
  normalized_path(NULL_LEX_STRING),
469
636
  block_size(0),
470
637
  version(0),
471
638
  timestamp_offset(0),
472
639
  reclength(0),
473
640
  stored_rec_length(0),
474
641
  max_rows(0),
475
 
  _table_message(NULL),
 
642
  table_proto(NULL),
476
643
  storage_engine(NULL),
477
644
  tmp_table(type_arg),
478
 
  _ref_count(0),
 
645
  ref_count(0),
479
646
  null_bytes(0),
480
647
  last_null_bit_pos(0),
481
 
  _field_size(0),
 
648
  fields(0),
482
649
  rec_buff_length(0),
483
650
  keys(0),
484
651
  key_parts(0),
488
655
  uniques(0),
489
656
  null_fields(0),
490
657
  blob_fields(0),
 
658
  timestamp_field_offset(0),
491
659
  has_variable_width(false),
492
660
  db_create_options(0),
493
661
  db_options_in_use(0),
500
668
  error(0),
501
669
  open_errno(0),
502
670
  errarg(0),
503
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
671
  blob_ptr_size(0),
504
672
  db_low_byte_first(false),
 
673
  name_lock(false),
 
674
  replace_with_name_lock(false),
 
675
  waiting_on_cond(false),
505
676
  keys_in_use(0),
506
 
  keys_for_keyread(0)
 
677
  keys_for_keyread(0),
 
678
  event_observers(NULL)
507
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
 
508
686
  char *path_buff;
509
687
  std::string _path;
510
688
 
524
702
  }
525
703
  else
526
704
  {
527
 
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
 
705
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
528
706
  }
529
707
 
530
708
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
538
716
  else
539
717
  {
540
718
    assert(0); // We should throw here.
541
 
    abort();
542
719
  }
543
720
}
544
721
 
559
736
 
560
737
TableShare::~TableShare() 
561
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
 
562
759
  storage_engine= NULL;
563
760
 
 
761
  delete table_proto;
 
762
  table_proto= NULL;
 
763
 
564
764
  mem_root.free_root(MYF(0));                 // Free's share
565
765
}
566
766
 
567
 
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
 
767
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
568
768
{
569
769
  private_key_for_cache= identifier_arg.getKey();
570
770
 
577
777
  table_name.str=    db.str + db.length + 1;
578
778
  table_name.length= strlen(table_name.str);
579
779
 
580
 
  getTableMessage()->set_name(identifier_arg.getTableName());
581
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
 
780
  table_proto->set_name(identifier_arg.getTableName());
 
781
  table_proto->set_schema(identifier_arg.getSchemaName());
582
782
}
583
783
 
584
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
 
784
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
585
785
{
586
 
  drizzled::error_t local_error= EE_OK;
 
786
  int local_error= 0;
587
787
 
588
788
  if (! table.IsInitialized())
589
789
  {
590
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
591
 
             table.name().empty() ? " " :  table.name().c_str(),
592
 
             table.InitializationErrorString().c_str());
593
 
 
 
790
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
791
    return ER_CORRUPT_TABLE_DEFINITION;
595
792
  }
596
793
 
597
 
  setTableMessage(table);
 
794
  setTableProto(new(nothrow) message::Table(table));
598
795
 
599
796
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
797
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
620
817
 
621
818
  table_charset= get_charset(table_options.collation_id());
622
819
 
623
 
  if (not table_charset)
 
820
  if (! table_charset)
624
821
  {
625
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
626
 
             table_options.collation().c_str(),
627
 
             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';
628
829
 
629
 
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
 
830
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
831
    return ER_CORRUPT_TABLE_DEFINITION;
630
832
  }
631
833
 
632
834
  db_record_offset= 1;
633
835
 
 
836
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
837
 
634
838
  keys= table.indexes_size();
635
839
 
636
840
  key_parts= 0;
780
984
  keys_for_keyread.reset();
781
985
  set_prefix(keys_in_use, keys);
782
986
 
783
 
  _field_size= table.field_size();
 
987
  fields= table.field_size();
784
988
 
785
 
  setFields(_field_size + 1);
786
 
  _fields[_field_size]= NULL;
 
989
  setFields(fields + 1);
 
990
  field[fields]= NULL;
787
991
 
788
992
  uint32_t local_null_fields= 0;
789
993
  reclength= 0;
790
994
 
791
 
  std::vector<uint32_t> field_offsets;
792
 
  std::vector<uint32_t> field_pack_length;
 
995
  vector<uint32_t> field_offsets;
 
996
  vector<uint32_t> field_pack_length;
793
997
 
794
 
  field_offsets.resize(_field_size);
795
 
  field_pack_length.resize(_field_size);
 
998
  field_offsets.resize(fields);
 
999
  field_pack_length.resize(fields);
796
1000
 
797
1001
  uint32_t interval_count= 0;
798
1002
  uint32_t interval_parts= 0;
799
1003
 
800
1004
  uint32_t stored_columns_reclength= 0;
801
1005
 
802
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1006
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
803
1007
  {
804
1008
    message::Table::Field pfield= table.field(fieldnr);
805
 
    if (pfield.constraints().is_nullable()) // Historical reference
806
 
    {
807
 
      local_null_fields++;
808
 
    }
809
 
    else if (not pfield.constraints().is_notnull())
810
 
    {
811
 
      local_null_fields++;
812
 
    }
 
1009
    if (pfield.constraints().is_nullable())
 
1010
      local_null_fields++;
813
1011
 
814
 
    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());
815
1014
 
816
1015
    field_offsets[fieldnr]= stored_columns_reclength;
817
1016
 
850
1049
      {
851
1050
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
852
1051
 
853
 
        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());
854
1053
      }
855
1054
      break;
856
1055
    default:
900
1099
 
901
1100
  uint32_t interval_nr= 0;
902
1101
 
903
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1102
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
904
1103
  {
905
1104
    message::Table::Field pfield= table.field(fieldnr);
906
1105
 
912
1111
 
913
1112
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
914
1113
    {
915
 
      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';
916
1120
 
917
 
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
 
1121
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
1122
      return ER_CORRUPT_TABLE_DEFINITION;
918
1123
    }
919
1124
 
920
1125
 
957
1162
  /* and read the fields */
958
1163
  interval_nr= 0;
959
1164
 
960
 
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
 
1165
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
961
1166
 
962
1167
  unsigned char* null_pos= getDefaultValues();
963
1168
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
964
1169
 
965
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1170
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
966
1171
  {
967
1172
    message::Table::Field pfield= table.field(fieldnr);
968
1173
 
988
1193
        unireg_type= Field::TIMESTAMP_DN_FIELD;
989
1194
      }
990
1195
      else
991
 
      {
992
 
        assert(0); // Invalid update value.
993
 
        abort();
994
 
      }
 
1196
        assert(1); // Invalid update value.
995
1197
    }
996
1198
    else if (pfield.has_options() &&
997
1199
             pfield.options().has_update_expression() &&
1017
1219
 
1018
1220
    enum_field_types field_type;
1019
1221
 
1020
 
    field_type= proto_field_type_to_drizzle_type(pfield);
 
1222
    field_type= proto_field_type_to_drizzle_type(pfield.type());
1021
1223
 
1022
1224
    const CHARSET_INFO *charset= &my_charset_bin;
1023
1225
 
1062
1264
      {
1063
1265
        if (fo.scale() > DECIMAL_MAX_SCALE)
1064
1266
        {
1065
 
          local_error= ER_NOT_FORM_FILE;
 
1267
          local_error= 4;
1066
1268
 
1067
 
          return true;
 
1269
          return local_error;
1068
1270
        }
1069
1271
        decimals= static_cast<uint8_t>(fo.scale());
1070
1272
      }
1084
1286
    }
1085
1287
 
1086
1288
 
 
1289
    db_low_byte_first= true; //Cursor->low_byte_first();
 
1290
    blob_ptr_size= portable_sizeof_char_ptr;
 
1291
 
1087
1292
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1088
1293
 
1089
 
    // We set field_length in this loop.
1090
1294
    switch (field_type)
1091
1295
    {
1092
1296
    case DRIZZLE_TYPE_BLOB:
1118
1322
            decimals != NOT_FIXED_DEC)
1119
1323
        {
1120
1324
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1121
 
          local_error= ER_M_BIGGER_THAN_D;
1122
 
          return true;
 
1325
          local_error= 1;
 
1326
 
 
1327
          return local_error;
1123
1328
        }
1124
1329
        break;
1125
1330
      }
1127
1332
      {
1128
1333
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1129
1334
 
1130
 
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1335
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1131
1336
                                                     false);
1132
1337
        break;
1133
1338
      }
 
1339
    case DRIZZLE_TYPE_TIMESTAMP:
1134
1340
    case DRIZZLE_TYPE_DATETIME:
1135
1341
      field_length= DateTime::MAX_STRING_LENGTH;
1136
1342
      break;
1163
1369
      }
1164
1370
      break;
1165
1371
    case DRIZZLE_TYPE_LONGLONG:
1166
 
      {
1167
 
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1168
 
        field_length= MAX_BIGINT_WIDTH+sign_len;
1169
 
      }
1170
 
      break;
1171
 
    case DRIZZLE_TYPE_UUID:
1172
 
      field_length= field::Uuid::max_string_length();
1173
 
      break;
1174
 
    case DRIZZLE_TYPE_BOOLEAN:
1175
 
      field_length= field::Boolean::max_string_length();
1176
 
      break;
1177
 
    case DRIZZLE_TYPE_MICROTIME:
1178
 
      field_length= field::Microtime::max_string_length();
1179
 
      break;
1180
 
    case DRIZZLE_TYPE_TIMESTAMP:
1181
 
      field_length= field::Epoch::max_string_length();
1182
 
      break;
1183
 
    case DRIZZLE_TYPE_TIME:
1184
 
      field_length= field::Time::max_string_length();
 
1372
      field_length= MAX_BIGINT_WIDTH;
1185
1373
      break;
1186
1374
    case DRIZZLE_TYPE_NULL:
1187
1375
      abort(); // Programming error
1188
1376
    }
1189
1377
 
1190
 
    bool is_not_null= false;
1191
 
 
1192
 
    if (not pfield.constraints().is_nullable())
1193
 
    {
1194
 
      is_not_null= true;
1195
 
    }
1196
 
    else if (pfield.constraints().is_notnull())
1197
 
    {
1198
 
      is_not_null= true;
1199
 
    }
1200
 
 
1201
 
    Field* f= make_field(pfield,
1202
 
                         record + field_offsets[fieldnr] + data_offset,
1203
 
                         field_length,
1204
 
                         not is_not_null,
1205
 
                         null_pos,
1206
 
                         null_bit_pos,
1207
 
                         decimals,
1208
 
                         field_type,
1209
 
                         charset,
1210
 
                         MTYP_TYPENR(unireg_type),
1211
 
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1212
 
                         getTableMessage()->field(fieldnr).name().c_str());
1213
 
 
1214
 
    _fields[fieldnr]= f;
1215
 
 
1216
 
    // Insert post make_field code here.
1217
 
    switch (field_type)
1218
 
    {
1219
 
    case DRIZZLE_TYPE_BLOB:
1220
 
    case DRIZZLE_TYPE_VARCHAR:
1221
 
    case DRIZZLE_TYPE_DOUBLE:
1222
 
    case DRIZZLE_TYPE_DECIMAL:
1223
 
    case DRIZZLE_TYPE_TIMESTAMP:
1224
 
    case DRIZZLE_TYPE_TIME:
1225
 
    case DRIZZLE_TYPE_DATETIME:
1226
 
    case DRIZZLE_TYPE_MICROTIME:
1227
 
    case DRIZZLE_TYPE_DATE:
1228
 
    case DRIZZLE_TYPE_ENUM:
1229
 
    case DRIZZLE_TYPE_LONG:
1230
 
    case DRIZZLE_TYPE_LONGLONG:
1231
 
    case DRIZZLE_TYPE_NULL:
1232
 
    case DRIZZLE_TYPE_UUID:
1233
 
    case DRIZZLE_TYPE_BOOLEAN:
1234
 
      break;
1235
 
    }
 
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;
1236
1393
 
1237
1394
    // This needs to go, we should be setting the "use" on the field so that
1238
1395
    // it does not reference the share/table.
1258
1415
      if (res != 0 && res != 3) /* @TODO Huh? */
1259
1416
      {
1260
1417
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1261
 
        local_error= ER_INVALID_DEFAULT;
 
1418
        local_error= 1;
1262
1419
 
1263
 
        return true;
 
1420
        return local_error;
1264
1421
      }
1265
1422
    }
1266
 
    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))
1267
1425
    {
1268
1426
      f->set_notnull();
1269
1427
      f->store((int64_t) 1, true);
1277
1435
    f->setTable(NULL);
1278
1436
    f->orig_table= NULL;
1279
1437
 
1280
 
    f->setPosition(fieldnr);
 
1438
    f->field_index= fieldnr;
1281
1439
    f->comment= comment;
1282
 
    if (not default_value &&
1283
 
        not (f->unireg_check==Field::NEXT_NUMBER) &&
 
1440
    if (! default_value &&
 
1441
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1284
1442
        (f->flags & NOT_NULL_FLAG) &&
1285
 
        (not f->is_timestamp()))
 
1443
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1286
1444
    {
1287
1445
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1288
1446
    }
1289
1447
 
1290
1448
    if (f->unireg_check == Field::NEXT_NUMBER)
1291
 
      found_next_number_field= &(_fields[fieldnr]);
 
1449
      found_next_number_field= &(field[fieldnr]);
 
1450
 
 
1451
    if (timestamp_field == f)
 
1452
      timestamp_field_offset= fieldnr;
1292
1453
 
1293
1454
    if (use_hash) /* supposedly this never fails... but comments lie */
1294
1455
    {
1295
 
      const char *local_field_name= _fields[fieldnr]->field_name;
1296
 
      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])));
1297
1458
    }
 
1459
 
1298
1460
  }
1299
1461
 
1300
1462
  keyinfo= key_info;
1319
1481
    We need to set the unused bits to 1. If the number of bits is a multiple
1320
1482
    of 8 there are no unused bits.
1321
1483
  */
 
1484
 
1322
1485
  if (null_count & 7)
1323
1486
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1324
1487
 
1349
1512
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1350
1513
        {
1351
1514
          uint32_t fieldnr= key_part[i].fieldnr;
1352
 
          if (not fieldnr ||
1353
 
              _fields[fieldnr-1]->null_ptr ||
1354
 
              _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)
1355
1518
          {
1356
1519
            local_primary_key= MAX_KEY; // Can't be used
1357
1520
            break;
1366
1529
        {
1367
1530
          return ENOMEM;
1368
1531
        }
1369
 
        local_field= key_part->field= _fields[key_part->fieldnr-1];
 
1532
        local_field= key_part->field= field[key_part->fieldnr-1];
1370
1533
        key_part->type= local_field->key_type();
1371
1534
        if (local_field->null_ptr)
1372
1535
        {
1472
1635
                            &next_number_keypart)) < 0)
1473
1636
    {
1474
1637
      /* Wrong field definition */
1475
 
      local_error= ER_NOT_FORM_FILE;
 
1638
      local_error= 4;
1476
1639
 
1477
 
      return true;
 
1640
      return local_error;
1478
1641
    }
1479
1642
    else
1480
1643
    {
1488
1651
    blob_field.resize(blob_fields);
1489
1652
    uint32_t *save= &blob_field[0];
1490
1653
    uint32_t k= 0;
1491
 
    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++)
1492
1655
    {
1493
1656
      if ((*iter)->flags & BLOB_FLAG)
1494
1657
        (*save++)= k;
1495
1658
    }
1496
1659
  }
1497
1660
 
 
1661
  db_low_byte_first= true; // @todo Question this.
1498
1662
  all_set.clear();
1499
 
  all_set.resize(_field_size);
 
1663
  all_set.resize(fields);
1500
1664
  all_set.set();
1501
1665
 
1502
 
  return local_error != EE_OK;
1503
 
}
 
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
 
1504
1684
 
1505
1685
/*
1506
1686
  Read table definition from a binary / text based .frm cursor
1526
1706
  6    Unknown .frm version
1527
1707
*/
1528
1708
 
1529
 
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
 
1709
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1530
1710
{
1531
 
  drizzled::error_t local_error= EE_OK;
1532
 
 
1533
 
  message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1534
 
 
1535
 
  if (table and table->IsInitialized())
 
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)
1536
1722
  {
1537
 
    if (parse_table_proto(session, *table))
 
1723
    if (local_error > 0)
1538
1724
    {
1539
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1725
      errno= local_error;
 
1726
      local_error= 1;
1541
1727
    }
1542
1728
    else
1543
1729
    {
1544
 
      setTableCategory(TABLE_CATEGORY_USER);
1545
 
      local_error= EE_OK;
 
1730
      if (not table.IsInitialized())
 
1731
      {
 
1732
        local_error= 4;
 
1733
      }
1546
1734
    }
1547
 
  }
1548
 
  else if (table and not table->IsInitialized())
1549
 
  {
1550
 
    local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1551
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1552
 
  }
1553
 
  else
1554
 
  {
1555
 
    local_error= ER_TABLE_UNKNOWN;
1556
 
    my_error(ER_TABLE_UNKNOWN, identifier);
1557
 
  }
1558
 
 
1559
 
  return static_cast<int>(local_error);
 
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);
1560
1750
}
1561
1751
 
1562
1752
 
1584
1774
  7    Table definition has changed in engine
1585
1775
*/
1586
1776
int TableShare::open_table_from_share(Session *session,
1587
 
                                      const identifier::Table &identifier,
 
1777
                                      const TableIdentifier &identifier,
1588
1778
                                      const char *alias,
1589
1779
                                      uint32_t db_stat, uint32_t ha_open_flags,
1590
1780
                                      Table &outparam)
1601
1791
  if (not error_reported)
1602
1792
    open_table_error(ret, errno, 0);
1603
1793
 
1604
 
  boost::checked_delete(outparam.cursor);
 
1794
  delete outparam.cursor;
1605
1795
  outparam.cursor= 0;                           // For easier error checking
1606
1796
  outparam.db_stat= 0;
1607
1797
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1620
1810
  unsigned char *record= NULL;
1621
1811
  Field **field_ptr;
1622
1812
 
 
1813
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1814
  assert(session->lex->is_lex_started);
 
1815
 
1623
1816
  local_error= 1;
1624
1817
  outparam.resetTable(session, this, db_stat);
1625
1818
 
1671
1864
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1672
1865
  }
1673
1866
 
1674
 
  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*)))))
1675
1868
  {
1676
1869
    return local_error;
1677
1870
  }
1683
1876
  outparam.null_flags= (unsigned char*) record+1;
1684
1877
 
1685
1878
  /* Setup copy of fields from share, but use the right alias and record */
1686
 
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
 
1879
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1687
1880
  {
1688
 
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
 
1881
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1689
1882
      return local_error;
1690
1883
  }
1691
1884
  (*field_ptr)= 0;                              // End marker
1694
1887
    outparam.found_next_number_field=
1695
1888
      outparam.getField(positionFields(found_next_number_field));
1696
1889
  if (timestamp_field)
1697
 
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
 
1890
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1891
 
1698
1892
 
1699
1893
  /* Fix key->name and key_part->field */
1700
1894
  if (key_parts)
1743
1937
 
1744
1938
  /* Allocate bitmaps */
1745
1939
 
1746
 
  outparam.def_read_set.resize(_field_size);
1747
 
  outparam.def_write_set.resize(_field_size);
1748
 
  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);
1749
1943
  outparam.default_column_bitmaps();
1750
1944
 
1751
1945
  return 0;
1752
1946
}
1753
1947
 
1754
 
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
 
1948
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1755
1949
                                        uint32_t db_stat, uint32_t ha_open_flags,
1756
1950
                                        Table &outparam,
1757
1951
                                        bool &error_reported)
1802
1996
/* error message when opening a form cursor */
1803
1997
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1804
1998
{
 
1999
  int err_no;
1805
2000
  char buff[FN_REFLEN];
1806
2001
  myf errortype= ME_ERROR+ME_WAITTANG;
1807
2002
 
1810
2005
  case 1:
1811
2006
    if (db_errno == ENOENT)
1812
2007
    {
1813
 
      identifier::Table identifier(db.str, table_name.str);
1814
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
2008
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1815
2009
    }
1816
2010
    else
1817
2011
    {
1822
2016
    break;
1823
2017
  case 2:
1824
2018
    {
1825
 
      drizzled::error_t err_no;
1826
 
 
1827
2019
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1828
2020
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1829
 
 
1830
2021
      my_error(err_no, errortype, normalized_path.str, db_errno);
1831
2022
      break;
1832
2023
    }
1862
2053
  return;
1863
2054
} /* open_table_error */
1864
2055
 
1865
 
Field *TableShare::make_field(const message::Table::Field &pfield,
1866
 
                              unsigned char *ptr,
 
2056
Field *TableShare::make_field(unsigned char *ptr,
1867
2057
                              uint32_t field_length,
1868
2058
                              bool is_nullable,
1869
2059
                              unsigned char *null_pos,
1875
2065
                              TYPELIB *interval,
1876
2066
                              const char *field_name)
1877
2067
{
1878
 
  return make_field(pfield,
1879
 
                    ptr,
1880
 
                    field_length,
1881
 
                    is_nullable,
1882
 
                    null_pos,
1883
 
                    null_bit,
1884
 
                    decimals,
1885
 
                    field_type,
1886
 
                    field_charset,
1887
 
                    unireg_check,
1888
 
                    interval,
1889
 
                    field_name,
1890
 
                    pfield.constraints().is_unsigned());
1891
 
}
1892
 
 
1893
 
Field *TableShare::make_field(const message::Table::Field &,
1894
 
                              unsigned char *ptr,
1895
 
                              uint32_t field_length,
1896
 
                              bool is_nullable,
1897
 
                              unsigned char *null_pos,
1898
 
                              unsigned char null_bit,
1899
 
                              uint8_t decimals,
1900
 
                              enum_field_types field_type,
1901
 
                              const CHARSET_INFO * field_charset,
1902
 
                              Field::utype unireg_check,
1903
 
                              TYPELIB *interval,
1904
 
                              const char *field_name, 
1905
 
                              bool is_unsigned)
1906
 
{
1907
2068
  if (! is_nullable)
1908
2069
  {
1909
2070
    null_pos=0;
1914
2075
    null_bit= ((unsigned char) 1) << null_bit;
1915
2076
  }
1916
2077
 
 
2078
  switch (field_type) 
 
2079
  {
 
2080
  case DRIZZLE_TYPE_DATE:
 
2081
  case DRIZZLE_TYPE_DATETIME:
 
2082
  case DRIZZLE_TYPE_TIMESTAMP:
 
2083
    field_charset= &my_charset_bin;
 
2084
  default: break;
 
2085
  }
 
2086
 
1917
2087
  switch (field_type)
1918
2088
  {
1919
2089
  case DRIZZLE_TYPE_ENUM:
1920
2090
    return new (&mem_root) Field_enum(ptr,
1921
 
                                      field_length,
1922
 
                                      null_pos,
1923
 
                                      null_bit,
1924
 
                                      field_name,
1925
 
                                      interval,
1926
 
                                      field_charset);
 
2091
                                 field_length,
 
2092
                                 null_pos,
 
2093
                                 null_bit,
 
2094
                                 field_name,
 
2095
                                 interval,
 
2096
                                 field_charset);
1927
2097
  case DRIZZLE_TYPE_VARCHAR:
1928
2098
    setVariableWidth();
1929
2099
    return new (&mem_root) Field_varstring(ptr,field_length,
1933
2103
                                      field_charset);
1934
2104
  case DRIZZLE_TYPE_BLOB:
1935
2105
    return new (&mem_root) Field_blob(ptr,
1936
 
                                      null_pos,
1937
 
                                      null_bit,
1938
 
                                      field_name,
1939
 
                                      this,
1940
 
                                      field_charset);
 
2106
                                 null_pos,
 
2107
                                 null_bit,
 
2108
                                 field_name,
 
2109
                                 this,
 
2110
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
2111
                                 field_charset);
1941
2112
  case DRIZZLE_TYPE_DECIMAL:
1942
2113
    return new (&mem_root) Field_decimal(ptr,
1943
 
                                         field_length,
1944
 
                                         null_pos,
1945
 
                                         null_bit,
1946
 
                                         unireg_check,
1947
 
                                         field_name,
1948
 
                                         decimals);
 
2114
                                    field_length,
 
2115
                                    null_pos,
 
2116
                                    null_bit,
 
2117
                                    unireg_check,
 
2118
                                    field_name,
 
2119
                                    decimals,
 
2120
                                    false,
 
2121
                                    false /* is_unsigned */);
1949
2122
  case DRIZZLE_TYPE_DOUBLE:
1950
2123
    return new (&mem_root) Field_double(ptr,
1951
2124
                                   field_length,
1956
2129
                                   decimals,
1957
2130
                                   false,
1958
2131
                                   false /* is_unsigned */);
1959
 
  case DRIZZLE_TYPE_UUID:
1960
 
    return new (&mem_root) field::Uuid(ptr,
1961
 
                                       field_length,
1962
 
                                       null_pos,
1963
 
                                       null_bit,
1964
 
                                       field_name);
1965
 
  case DRIZZLE_TYPE_BOOLEAN:
1966
 
    return new (&mem_root) field::Boolean(ptr,
1967
 
                                          field_length,
1968
 
                                          null_pos,
1969
 
                                          null_bit,
1970
 
                                          field_name,
1971
 
                                          is_unsigned);
1972
2132
  case DRIZZLE_TYPE_LONG:
1973
 
    return new (&mem_root) field::Int32(ptr,
1974
 
                                        field_length,
1975
 
                                        null_pos,
1976
 
                                        null_bit,
1977
 
                                        unireg_check,
1978
 
                                        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 */);
1979
2141
  case DRIZZLE_TYPE_LONGLONG:
1980
 
    {
1981
 
      if (is_unsigned)
1982
 
      {
1983
 
        return new (&mem_root) field::Size(ptr,
1984
 
                                           field_length,
1985
 
                                           null_pos,
1986
 
                                           null_bit,
1987
 
                                           unireg_check,
1988
 
                                           field_name);
1989
 
      }
1990
 
 
1991
 
      return new (&mem_root) field::Int64(ptr,
1992
 
                                          field_length,
1993
 
                                          null_pos,
1994
 
                                          null_bit,
1995
 
                                          unireg_check,
1996
 
                                          field_name);
1997
 
    }
1998
 
  case DRIZZLE_TYPE_MICROTIME:
1999
 
    return new (&mem_root) field::Microtime(ptr,
2000
 
                                            null_pos,
2001
 
                                            null_bit,
2002
 
                                            unireg_check,
2003
 
                                            field_name,
2004
 
                                            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 */);
2005
2150
  case DRIZZLE_TYPE_TIMESTAMP:
2006
 
    return new (&mem_root) field::Epoch(ptr,
2007
 
                                        null_pos,
2008
 
                                        null_bit,
2009
 
                                        unireg_check,
2010
 
                                        field_name,
2011
 
                                        this);
2012
 
  case DRIZZLE_TYPE_TIME:
2013
 
    return new (&mem_root) field::Time(ptr,
2014
 
                                       field_length,
2015
 
                                       null_pos,
2016
 
                                       null_bit,
2017
 
                                       field_name);
 
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);
2018
2159
  case DRIZZLE_TYPE_DATE:
2019
2160
    return new (&mem_root) Field_date(ptr,
2020
2161
                                 null_pos,
2021
2162
                                 null_bit,
2022
 
                                 field_name);
 
2163
                                 field_name,
 
2164
                                 field_charset);
2023
2165
  case DRIZZLE_TYPE_DATETIME:
2024
2166
    return new (&mem_root) Field_datetime(ptr,
2025
2167
                                     null_pos,
2026
2168
                                     null_bit,
2027
 
                                     field_name);
 
2169
                                     field_name,
 
2170
                                     field_charset);
2028
2171
  case DRIZZLE_TYPE_NULL:
2029
2172
    return new (&mem_root) Field_null(ptr,
2030
 
                                      field_length,
2031
 
                                      field_name);
 
2173
                                 field_length,
 
2174
                                 field_name,
 
2175
                                 field_charset);
 
2176
  default: // Impossible (Wrong version)
 
2177
    break;
2032
2178
  }
2033
 
  assert(0);
2034
 
  abort();
2035
 
}
2036
 
 
2037
 
void TableShare::refreshVersion()
2038
 
{
2039
 
  version= refresh_version;
 
2179
  return 0;
2040
2180
}
2041
2181
 
2042
2182