~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* -*- mode: c++; c-basic-offset: 2; i/dent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License as published by
25
25
*/
26
26
 
27
27
/* Basic functions needed by many modules */
28
 
#include "config.h"
 
28
#include <config.h>
29
29
 
30
30
#include <pthread.h>
31
31
#include <float.h>
37
37
 
38
38
#include <cassert>
39
39
 
40
 
#include "drizzled/error.h"
41
 
#include "drizzled/gettext.h"
42
 
#include "drizzled/sql_base.h"
43
 
#include "drizzled/pthread_globals.h"
44
 
#include "drizzled/internal/my_pthread.h"
45
 
#include "drizzled/plugin/event_observer.h"
46
 
 
47
 
#include "drizzled/table.h"
48
 
#include "drizzled/table/shell.h"
49
 
 
50
 
#include "drizzled/session.h"
51
 
 
52
 
#include "drizzled/charset.h"
53
 
#include "drizzled/internal/m_string.h"
54
 
#include "drizzled/internal/my_sys.h"
55
 
 
56
 
#include "drizzled/item/string.h"
57
 
#include "drizzled/item/int.h"
58
 
#include "drizzled/item/decimal.h"
59
 
#include "drizzled/item/float.h"
60
 
#include "drizzled/item/null.h"
61
 
#include "drizzled/temporal.h"
62
 
 
63
 
#include "drizzled/field.h"
64
 
#include "drizzled/field/str.h"
65
 
#include "drizzled/field/num.h"
66
 
#include "drizzled/field/blob.h"
67
 
#include "drizzled/field/enum.h"
68
 
#include "drizzled/field/null.h"
69
 
#include "drizzled/field/date.h"
70
 
#include "drizzled/field/decimal.h"
71
 
#include "drizzled/field/real.h"
72
 
#include "drizzled/field/double.h"
73
 
#include "drizzled/field/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"
 
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>
79
90
 
80
91
using namespace std;
81
92
 
83
94
{
84
95
 
85
96
extern size_t table_def_size;
86
 
static TableDefinitionCache table_def_cache;
87
 
 
88
 
/*****************************************************************************
89
 
  Functions to handle table definition cach (TableShare)
90
 
 *****************************************************************************/
91
 
 
92
 
 
93
 
// @todo switch this a boost::thread one only call.
94
 
void TableShare::cacheStart(void)
95
 
{
96
 
  /* 
97
 
   * This is going to overalloc a bit - as rehash sets the number of
98
 
   * buckets, not the number of elements. BUT, it'll allow us to not need
99
 
   * to rehash later on as the unordered_map grows.
100
 
 */
101
 
  table_def_cache.rehash(table_def_size);
102
 
}
103
 
 
104
 
 
105
 
/**
106
 
 * @TODO This should return size_t
107
 
 */
108
 
uint32_t cached_table_definitions(void)
109
 
{
110
 
  return static_cast<uint32_t>(table_def_cache.size());
111
 
}
112
 
 
113
 
 
114
 
/*
115
 
  Mark that we are not using table share anymore.
116
 
 
117
 
  SYNOPSIS
118
 
  release()
119
 
  share         Table share
120
 
 
121
 
  IMPLEMENTATION
122
 
  If ref_count goes to zero and (we have done a refresh or if we have
123
 
  already too many open table shares) then delete the definition.
124
 
*/
125
 
 
126
 
void TableShare::release(TableShare *share)
127
 
{
128
 
  bool to_be_deleted= false;
129
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
130
 
 
131
 
  share->lock();
132
 
  if (!--share->ref_count)
133
 
  {
134
 
    to_be_deleted= true;
135
 
  }
136
 
 
137
 
  if (to_be_deleted)
138
 
  {
139
 
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
140
 
    plugin::EventObserver::deregisterTableEvents(*share);
141
 
   
142
 
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
143
 
    if (iter != table_def_cache.end())
144
 
    {
145
 
      table_def_cache.erase(iter);
146
 
    }
147
 
    return;
148
 
  }
149
 
  share->unlock();
150
 
}
151
 
 
152
 
void TableShare::release(TableSharePtr &share)
153
 
{
154
 
  bool to_be_deleted= false;
155
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
156
 
 
157
 
  share->lock();
158
 
  if (!--share->ref_count)
159
 
  {
160
 
    to_be_deleted= true;
161
 
  }
162
 
 
163
 
  if (to_be_deleted)
164
 
  {
165
 
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
166
 
    plugin::EventObserver::deregisterTableEvents(*share);
167
 
   
168
 
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
169
 
    if (iter != table_def_cache.end())
170
 
    {
171
 
      table_def_cache.erase(iter);
172
 
    }
173
 
    return;
174
 
  }
175
 
  share->unlock();
176
 
}
177
 
 
178
 
void TableShare::release(TableIdentifier &identifier)
179
 
{
180
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
181
 
  if (iter != table_def_cache.end())
182
 
  {
183
 
    TableSharePtr share= (*iter).second;
184
 
    share->version= 0;                          // Mark for delete
185
 
    if (share->ref_count == 0)
186
 
    {
187
 
      share->lock();
188
 
      plugin::EventObserver::deregisterTableEvents(*share);
189
 
      table_def_cache.erase(identifier.getKey());
190
 
    }
191
 
  }
192
 
}
193
 
 
194
 
 
195
 
static TableSharePtr foundTableShare(TableSharePtr share)
196
 
{
197
 
  /*
198
 
    We found an existing table definition. Return it if we didn't get
199
 
    an error when reading the table definition from file.
200
 
  */
201
 
 
202
 
  /* We must do a lock to ensure that the structure is initialized */
203
 
  if (share->error)
204
 
  {
205
 
    /* Table definition contained an error */
206
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
207
 
 
208
 
    return TableSharePtr();
209
 
  }
210
 
 
211
 
  share->incrementTableCount();
212
 
 
213
 
  return share;
214
 
}
215
 
 
216
 
/*
217
 
  Get TableShare for a table.
218
 
 
219
 
  get_table_share()
220
 
  session                       Thread handle
221
 
  table_list            Table that should be opened
222
 
  key                   Table cache key
223
 
  key_length            Length of key
224
 
  error                 out: Error code from open_table_def()
225
 
 
226
 
  IMPLEMENTATION
227
 
  Get a table definition from the table definition cache.
228
 
  If it doesn't exist, create a new from the table definition file.
229
 
 
230
 
  NOTES
231
 
  We must have wrlock on LOCK_open when we come here
232
 
  (To be changed later)
233
 
 
234
 
  RETURN
235
 
  0  Error
236
 
#  Share for table
237
 
*/
238
 
 
239
 
TableSharePtr TableShare::getShareCreate(Session *session, 
240
 
                                         TableIdentifier &identifier,
241
 
                                         int *error)
242
 
{
243
 
  TableSharePtr share;
244
 
 
245
 
  *error= 0;
246
 
 
247
 
  /* Read table definition from cache */
248
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
249
 
  if (iter != table_def_cache.end())
250
 
  {
251
 
    share= (*iter).second;
252
 
    return foundTableShare(share);
253
 
  }
254
 
 
255
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
256
 
  
257
 
  /*
258
 
    Lock mutex to be able to read table definition from file without
259
 
    conflicts
260
 
  */
261
 
  share->lock();
262
 
 
263
 
  pair<TableDefinitionCache::iterator, bool> ret=
264
 
    table_def_cache.insert(make_pair(identifier.getKey(), share));
265
 
  if (ret.second == false)
266
 
  {
267
 
    return TableSharePtr();
268
 
  }
269
 
 
270
 
  if (share->open_table_def(*session, identifier))
271
 
  {
272
 
    *error= share->error;
273
 
    table_def_cache.erase(identifier.getKey());
274
 
 
275
 
    return TableSharePtr();
276
 
  }
277
 
  share->ref_count++;                           // Mark in use
278
 
  
279
 
  plugin::EventObserver::registerTableEvents(*share);
280
 
  
281
 
  share->unlock();
282
 
 
283
 
  return share;
284
 
}
285
 
 
286
 
 
287
 
/*
288
 
  Check if table definition exits in cache
289
 
 
290
 
  SYNOPSIS
291
 
  get_cached_table_share()
292
 
  db                    Database name
293
 
  table_name            Table name
294
 
 
295
 
  RETURN
296
 
  0  Not cached
297
 
#  TableShare for table
298
 
*/
299
 
TableSharePtr TableShare::getShare(TableIdentifier &identifier)
300
 
{
301
 
  safe_mutex_assert_owner(LOCK_open.native_handle);
302
 
 
303
 
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
304
 
  if (iter != table_def_cache.end())
305
 
  {
306
 
    return (*iter).second;
307
 
  }
308
 
 
309
 
  return TableSharePtr();
310
 
}
311
 
 
312
 
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
313
 
{
314
 
  enum_field_types field_type;
315
 
 
316
 
  switch(proto_field_type)
 
97
 
 
98
 
 
99
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
 
100
{
 
101
  switch(field.type())
317
102
  {
318
103
  case message::Table::Field::INTEGER:
319
 
    field_type= DRIZZLE_TYPE_LONG;
320
 
    break;
 
104
    return DRIZZLE_TYPE_LONG;
 
105
 
321
106
  case message::Table::Field::DOUBLE:
322
 
    field_type= DRIZZLE_TYPE_DOUBLE;
323
 
    break;
324
 
  case message::Table::Field::TIMESTAMP:
325
 
    field_type= DRIZZLE_TYPE_TIMESTAMP;
326
 
    break;
 
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
 
327
115
  case message::Table::Field::BIGINT:
328
 
    field_type= DRIZZLE_TYPE_LONGLONG;
329
 
    break;
 
116
    return DRIZZLE_TYPE_LONGLONG;
 
117
 
330
118
  case message::Table::Field::DATETIME:
331
 
    field_type= DRIZZLE_TYPE_DATETIME;
332
 
    break;
 
119
    return DRIZZLE_TYPE_DATETIME;
 
120
 
333
121
  case message::Table::Field::DATE:
334
 
    field_type= DRIZZLE_TYPE_DATE;
335
 
    break;
 
122
    return DRIZZLE_TYPE_DATE;
 
123
 
336
124
  case message::Table::Field::VARCHAR:
337
 
    field_type= DRIZZLE_TYPE_VARCHAR;
338
 
    break;
 
125
    return DRIZZLE_TYPE_VARCHAR;
 
126
 
339
127
  case message::Table::Field::DECIMAL:
340
 
    field_type= DRIZZLE_TYPE_DECIMAL;
341
 
    break;
 
128
    return DRIZZLE_TYPE_DECIMAL;
 
129
 
342
130
  case message::Table::Field::ENUM:
343
 
    field_type= DRIZZLE_TYPE_ENUM;
344
 
    break;
 
131
    return DRIZZLE_TYPE_ENUM;
 
132
 
345
133
  case message::Table::Field::BLOB:
346
 
    field_type= DRIZZLE_TYPE_BLOB;
347
 
    break;
348
 
  default:
349
 
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
350
 
    assert(1);
 
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;
351
144
  }
352
145
 
353
 
  return field_type;
 
146
  abort();
354
147
}
355
148
 
356
149
static Item *default_value_item(enum_field_types field_type,
381
174
                                 default_value->length());
382
175
    break;
383
176
  case DRIZZLE_TYPE_NULL:
384
 
    assert(false);
 
177
    assert(0);
 
178
    abort();
385
179
  case DRIZZLE_TYPE_TIMESTAMP:
386
180
  case DRIZZLE_TYPE_DATETIME:
 
181
  case DRIZZLE_TYPE_TIME:
387
182
  case DRIZZLE_TYPE_DATE:
388
183
  case DRIZZLE_TYPE_ENUM:
 
184
  case DRIZZLE_TYPE_UUID:
 
185
  case DRIZZLE_TYPE_MICROTIME:
 
186
  case DRIZZLE_TYPE_BOOLEAN:
389
187
    default_item= new Item_string(default_value->c_str(),
390
188
                                  default_value->length(),
391
189
                                  system_charset_info);
424
222
 */
425
223
bool TableShare::fieldInPrimaryKey(Field *in_field) const
426
224
{
427
 
  assert(table_proto != NULL);
 
225
  assert(getTableMessage());
428
226
 
429
 
  size_t num_indexes= table_proto->indexes_size();
 
227
  size_t num_indexes= getTableMessage()->indexes_size();
430
228
 
431
229
  for (size_t x= 0; x < num_indexes; ++x)
432
230
  {
433
 
    const message::Table::Index &index= table_proto->indexes(x);
 
231
    const message::Table::Index &index= getTableMessage()->indexes(x);
434
232
    if (index.is_primary())
435
233
    {
436
234
      size_t num_parts= index.index_part_size();
437
235
      for (size_t y= 0; y < num_parts; ++y)
438
236
      {
439
 
        if (index.index_part(y).fieldnr() == in_field->field_index)
 
237
        if (index.index_part(y).fieldnr() == in_field->position())
440
238
          return true;
441
239
      }
442
240
    }
444
242
  return false;
445
243
}
446
244
 
447
 
const TableDefinitionCache &TableShare::getCache()
448
 
{
449
 
  return table_def_cache;
450
 
}
451
 
 
452
 
TableShare::TableShare(TableIdentifier::Type type_arg) :
 
245
TableShare::TableShare(const identifier::Table::Type type_arg) :
453
246
  table_category(TABLE_UNKNOWN_CATEGORY),
454
 
  open_count(0),
455
247
  found_next_number_field(NULL),
456
248
  timestamp_field(NULL),
457
249
  key_info(NULL),
458
250
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
459
251
  all_set(),
 
252
  db(NULL_LEX_STRING),
 
253
  table_name(NULL_LEX_STRING),
 
254
  path(NULL_LEX_STRING),
 
255
  normalized_path(NULL_LEX_STRING),
460
256
  block_size(0),
461
257
  version(0),
462
258
  timestamp_offset(0),
463
259
  reclength(0),
464
260
  stored_rec_length(0),
465
261
  max_rows(0),
466
 
  table_proto(NULL),
 
262
  _table_message(NULL),
467
263
  storage_engine(NULL),
468
264
  tmp_table(type_arg),
469
 
  ref_count(0),
 
265
  _ref_count(0),
470
266
  null_bytes(0),
471
267
  last_null_bit_pos(0),
472
 
  fields(0),
 
268
  _field_size(0),
473
269
  rec_buff_length(0),
474
270
  keys(0),
475
271
  key_parts(0),
479
275
  uniques(0),
480
276
  null_fields(0),
481
277
  blob_fields(0),
482
 
  timestamp_field_offset(0),
483
278
  has_variable_width(false),
484
279
  db_create_options(0),
485
280
  db_options_in_use(0),
492
287
  error(0),
493
288
  open_errno(0),
494
289
  errarg(0),
495
 
  blob_ptr_size(0),
 
290
  blob_ptr_size(portable_sizeof_char_ptr),
496
291
  db_low_byte_first(false),
497
 
  name_lock(false),
498
 
  replace_with_name_lock(false),
499
 
  waiting_on_cond(false),
500
292
  keys_in_use(0),
501
 
  keys_for_keyread(0),
502
 
  event_observers(NULL)
 
293
  keys_for_keyread(0)
503
294
{
504
 
 
505
 
  table_charset= 0;
506
 
  memset(&db, 0, sizeof(LEX_STRING));
507
 
  memset(&table_name, 0, sizeof(LEX_STRING));
508
 
  memset(&path, 0, sizeof(LEX_STRING));
509
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
510
 
 
511
295
  if (type_arg == message::Table::INTERNAL)
512
296
  {
513
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache);
514
 
    init(&private_key_for_cache[0], &private_key_for_cache[0]);
 
297
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
298
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
515
299
  }
516
300
  else
517
301
  {
519
303
  }
520
304
}
521
305
 
522
 
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
 
306
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
523
307
  table_category(TABLE_UNKNOWN_CATEGORY),
524
 
  open_count(0),
525
308
  found_next_number_field(NULL),
526
309
  timestamp_field(NULL),
527
310
  key_info(NULL),
528
311
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
312
  table_charset(0),
529
313
  all_set(),
 
314
  db(NULL_LEX_STRING),
 
315
  table_name(NULL_LEX_STRING),
 
316
  path(NULL_LEX_STRING),
 
317
  normalized_path(NULL_LEX_STRING),
530
318
  block_size(0),
531
319
  version(0),
532
320
  timestamp_offset(0),
533
321
  reclength(0),
534
322
  stored_rec_length(0),
535
323
  max_rows(0),
536
 
  table_proto(NULL),
 
324
  _table_message(NULL),
537
325
  storage_engine(NULL),
538
326
  tmp_table(message::Table::INTERNAL),
539
 
  ref_count(0),
 
327
  _ref_count(0),
540
328
  null_bytes(0),
541
329
  last_null_bit_pos(0),
542
 
  fields(0),
 
330
  _field_size(0),
543
331
  rec_buff_length(0),
544
332
  keys(0),
545
333
  key_parts(0),
549
337
  uniques(0),
550
338
  null_fields(0),
551
339
  blob_fields(0),
552
 
  timestamp_field_offset(0),
553
340
  has_variable_width(false),
554
341
  db_create_options(0),
555
342
  db_options_in_use(0),
562
349
  error(0),
563
350
  open_errno(0),
564
351
  errarg(0),
565
 
  blob_ptr_size(0),
 
352
  blob_ptr_size(portable_sizeof_char_ptr),
566
353
  db_low_byte_first(false),
567
 
  name_lock(false),
568
 
  replace_with_name_lock(false),
569
 
  waiting_on_cond(false),
570
354
  keys_in_use(0),
571
 
  keys_for_keyread(0),
572
 
  event_observers(NULL)
 
355
  keys_for_keyread(0)
573
356
{
574
357
  assert(identifier.getKey() == key);
575
358
 
576
 
  table_charset= 0;
577
 
  memset(&path, 0, sizeof(LEX_STRING));
578
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
579
 
 
580
359
  private_key_for_cache= key;
581
360
 
582
361
  table_category=         TABLE_CATEGORY_TEMPORARY;
583
362
  tmp_table=              message::Table::INTERNAL;
584
363
 
585
 
  db.str= &private_key_for_cache[0];
586
 
  db.length= strlen(&private_key_for_cache[0]);
 
364
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
365
  db.length= strlen(private_key_for_cache.vector());
587
366
 
588
 
  table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
 
367
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
589
368
  table_name.length= strlen(table_name.str);
590
369
  path.str= (char *)"";
591
370
  normalized_path.str= path.str;
592
371
  path.length= normalized_path.length= 0;
593
 
  assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 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
 
594
377
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
595
378
}
596
379
 
597
380
 
598
 
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
 
381
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
599
382
  table_category(TABLE_UNKNOWN_CATEGORY),
600
 
  open_count(0),
601
383
  found_next_number_field(NULL),
602
384
  timestamp_field(NULL),
603
385
  key_info(NULL),
604
386
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
387
  table_charset(0),
605
388
  all_set(),
 
389
  db(NULL_LEX_STRING),
 
390
  table_name(NULL_LEX_STRING),
 
391
  path(NULL_LEX_STRING),
 
392
  normalized_path(NULL_LEX_STRING),
606
393
  block_size(0),
607
394
  version(0),
608
395
  timestamp_offset(0),
609
396
  reclength(0),
610
397
  stored_rec_length(0),
611
398
  max_rows(0),
612
 
  table_proto(NULL),
 
399
  _table_message(NULL),
613
400
  storage_engine(NULL),
614
401
  tmp_table(identifier.getType()),
615
 
  ref_count(0),
 
402
  _ref_count(0),
616
403
  null_bytes(0),
617
404
  last_null_bit_pos(0),
618
 
  fields(0),
 
405
  _field_size(0),
619
406
  rec_buff_length(0),
620
407
  keys(0),
621
408
  key_parts(0),
625
412
  uniques(0),
626
413
  null_fields(0),
627
414
  blob_fields(0),
628
 
  timestamp_field_offset(0),
629
415
  has_variable_width(false),
630
416
  db_create_options(0),
631
417
  db_options_in_use(0),
638
424
  error(0),
639
425
  open_errno(0),
640
426
  errarg(0),
641
 
  blob_ptr_size(0),
 
427
  blob_ptr_size(portable_sizeof_char_ptr),
642
428
  db_low_byte_first(false),
643
 
  name_lock(false),
644
 
  replace_with_name_lock(false),
645
 
  waiting_on_cond(false),
646
429
  keys_in_use(0),
647
 
  keys_for_keyread(0),
648
 
  event_observers(NULL)
 
430
  keys_for_keyread(0)
649
431
{
650
 
  table_charset= 0;
651
 
  memset(&db, 0, sizeof(LEX_STRING));
652
 
  memset(&table_name, 0, sizeof(LEX_STRING));
653
 
  memset(&path, 0, sizeof(LEX_STRING));
654
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
655
 
 
656
432
  private_key_for_cache= identifier.getKey();
657
433
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
658
434
  private_normalized_path.resize(identifier.getPath().size() + 1);
661
437
  {
662
438
    table_category=         TABLE_CATEGORY_TEMPORARY;
663
439
    tmp_table=              message::Table::INTERNAL;
664
 
    db.str= &private_key_for_cache[0];
665
 
    db.length= strlen(&private_key_for_cache[0]);
 
440
    db.str= const_cast<char *>(private_key_for_cache.vector());
 
441
    db.length= strlen(private_key_for_cache.vector());
666
442
    table_name.str= db.str + 1;
667
443
    table_name.length= strlen(table_name.str);
668
444
    path.str= &private_normalized_path[0];
675
451
/*
676
452
  Used for shares that will go into the cache.
677
453
*/
678
 
TableShare::TableShare(TableIdentifier::Type type_arg,
679
 
                       TableIdentifier &identifier,
 
454
TableShare::TableShare(const identifier::Table::Type type_arg,
 
455
                       const identifier::Table &identifier,
680
456
                       char *path_arg,
681
457
                       uint32_t path_length_arg) :
682
458
  table_category(TABLE_UNKNOWN_CATEGORY),
683
 
  open_count(0),
684
459
  found_next_number_field(NULL),
685
460
  timestamp_field(NULL),
686
461
  key_info(NULL),
687
462
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
463
  table_charset(0),
688
464
  all_set(),
 
465
  db(NULL_LEX_STRING),
 
466
  table_name(NULL_LEX_STRING),
 
467
  path(NULL_LEX_STRING),
 
468
  normalized_path(NULL_LEX_STRING),
689
469
  block_size(0),
690
470
  version(0),
691
471
  timestamp_offset(0),
692
472
  reclength(0),
693
473
  stored_rec_length(0),
694
474
  max_rows(0),
695
 
  table_proto(NULL),
 
475
  _table_message(NULL),
696
476
  storage_engine(NULL),
697
477
  tmp_table(type_arg),
698
 
  ref_count(0),
 
478
  _ref_count(0),
699
479
  null_bytes(0),
700
480
  last_null_bit_pos(0),
701
 
  fields(0),
 
481
  _field_size(0),
702
482
  rec_buff_length(0),
703
483
  keys(0),
704
484
  key_parts(0),
708
488
  uniques(0),
709
489
  null_fields(0),
710
490
  blob_fields(0),
711
 
  timestamp_field_offset(0),
712
491
  has_variable_width(false),
713
492
  db_create_options(0),
714
493
  db_options_in_use(0),
721
500
  error(0),
722
501
  open_errno(0),
723
502
  errarg(0),
724
 
  blob_ptr_size(0),
 
503
  blob_ptr_size(portable_sizeof_char_ptr),
725
504
  db_low_byte_first(false),
726
 
  name_lock(false),
727
 
  replace_with_name_lock(false),
728
 
  waiting_on_cond(false),
729
505
  keys_in_use(0),
730
 
  keys_for_keyread(0),
731
 
  event_observers(NULL)
 
506
  keys_for_keyread(0)
732
507
{
733
 
  table_charset= 0;
734
 
  memset(&db, 0, sizeof(LEX_STRING));
735
 
  memset(&table_name, 0, sizeof(LEX_STRING));
736
 
  memset(&path, 0, sizeof(LEX_STRING));
737
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
738
 
 
739
508
  char *path_buff;
740
509
  std::string _path;
741
510
 
744
513
    Let us use the fact that the key is "db/0/table_name/0" + optional
745
514
    part for temporary tables.
746
515
  */
747
 
  db.str= &private_key_for_cache[0];
 
516
  db.str= const_cast<char *>(private_key_for_cache.vector());
748
517
  db.length=         strlen(db.str);
749
518
  table_name.str=    db.str + db.length + 1;
750
519
  table_name.length= strlen(table_name.str);
755
524
  }
756
525
  else
757
526
  {
758
 
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
 
527
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
759
528
  }
760
529
 
761
530
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
769
538
  else
770
539
  {
771
540
    assert(0); // We should throw here.
 
541
    abort();
772
542
  }
773
543
}
774
544
 
789
559
 
790
560
TableShare::~TableShare() 
791
561
{
792
 
  assert(ref_count == 0);
793
 
 
794
 
  /*
795
 
    If someone is waiting for this to be deleted, inform it about this.
796
 
    Don't do a delete until we know that no one is refering to this anymore.
797
 
  */
798
 
  if (tmp_table == message::Table::STANDARD)
799
 
  {
800
 
    /* share->mutex is locked in release_table_share() */
801
 
    while (waiting_on_cond)
802
 
    {
803
 
      cond.notify_all();
804
 
      boost::mutex::scoped_lock scoped(mutex, boost::adopt_lock_t());
805
 
      cond.wait(scoped);
806
 
      scoped.release();
807
 
    }
808
 
    /* No thread refers to this anymore */
809
 
    mutex.unlock();
810
 
  }
811
 
 
812
562
  storage_engine= NULL;
813
563
 
814
 
  delete table_proto;
815
 
  table_proto= NULL;
816
 
 
817
564
  mem_root.free_root(MYF(0));                 // Free's share
818
565
}
819
566
 
820
 
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
 
567
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
821
568
{
822
 
  private_key_for_cache.clear();
823
569
  private_key_for_cache= identifier_arg.getKey();
824
570
 
825
571
  /*
826
572
    Let us use the fact that the key is "db/0/table_name/0" + optional
827
573
    part for temporary tables.
828
574
  */
829
 
  db.str= &private_key_for_cache[0];
 
575
  db.str= const_cast<char *>(private_key_for_cache.vector());
830
576
  db.length=         strlen(db.str);
831
577
  table_name.str=    db.str + db.length + 1;
832
578
  table_name.length= strlen(table_name.str);
833
579
 
834
 
  table_proto->set_name(identifier_arg.getTableName());
835
 
  table_proto->set_schema(identifier_arg.getSchemaName());
 
580
  getTableMessage()->set_name(identifier_arg.getTableName());
 
581
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
836
582
}
837
583
 
838
 
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
 
584
bool TableShare::parse_table_proto(Session& session, message::Table &table)
839
585
{
840
 
  int local_error= 0;
 
586
  drizzled::error_t local_error= EE_OK;
841
587
 
842
588
  if (! table.IsInitialized())
843
589
  {
844
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
 
590
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
591
             table.name().empty() ? " " :  table.name().c_str(),
 
592
             table.InitializationErrorString().c_str());
 
593
 
845
594
    return ER_CORRUPT_TABLE_DEFINITION;
846
595
  }
847
596
 
848
 
  setTableProto(new(nothrow) message::Table(table));
 
597
  setTableMessage(table);
849
598
 
850
599
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
851
600
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
871
620
 
872
621
  table_charset= get_charset(table_options.collation_id());
873
622
 
874
 
  if (! table_charset)
 
623
  if (not table_charset)
875
624
  {
876
 
    char errmsg[100];
877
 
    snprintf(errmsg, sizeof(errmsg),
878
 
             _("Table %s has invalid/unknown collation: %d,%s"),
879
 
             getPath(),
880
 
             table_options.collation_id(),
881
 
             table_options.collation().c_str());
882
 
    errmsg[99]='\0';
 
625
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
 
626
             table_options.collation().c_str(),
 
627
             table.name().c_str());
883
628
 
884
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
885
 
    return ER_CORRUPT_TABLE_DEFINITION;
 
629
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
886
630
  }
887
631
 
888
632
  db_record_offset= 1;
889
633
 
890
 
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
891
 
 
892
634
  keys= table.indexes_size();
893
635
 
894
636
  key_parts= 0;
1038
780
  keys_for_keyread.reset();
1039
781
  set_prefix(keys_in_use, keys);
1040
782
 
1041
 
  fields= table.field_size();
 
783
  _field_size= table.field_size();
1042
784
 
1043
 
  setFields(fields + 1);
1044
 
  field[fields]= NULL;
 
785
  setFields(_field_size + 1);
 
786
  _fields[_field_size]= NULL;
1045
787
 
1046
788
  uint32_t local_null_fields= 0;
1047
789
  reclength= 0;
1048
790
 
1049
 
  vector<uint32_t> field_offsets;
1050
 
  vector<uint32_t> field_pack_length;
 
791
  std::vector<uint32_t> field_offsets;
 
792
  std::vector<uint32_t> field_pack_length;
1051
793
 
1052
 
  field_offsets.resize(fields);
1053
 
  field_pack_length.resize(fields);
 
794
  field_offsets.resize(_field_size);
 
795
  field_pack_length.resize(_field_size);
1054
796
 
1055
797
  uint32_t interval_count= 0;
1056
798
  uint32_t interval_parts= 0;
1057
799
 
1058
800
  uint32_t stored_columns_reclength= 0;
1059
801
 
1060
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
802
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1061
803
  {
1062
804
    message::Table::Field pfield= table.field(fieldnr);
1063
 
    if (pfield.constraints().is_nullable())
1064
 
      local_null_fields++;
 
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
    }
1065
813
 
1066
 
    enum_field_types drizzle_field_type=
1067
 
      proto_field_type_to_drizzle_type(pfield.type());
 
814
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
1068
815
 
1069
816
    field_offsets[fieldnr]= stored_columns_reclength;
1070
817
 
1103
850
      {
1104
851
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1105
852
 
1106
 
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
 
853
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
1107
854
      }
1108
855
      break;
1109
856
    default:
1153
900
 
1154
901
  uint32_t interval_nr= 0;
1155
902
 
1156
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
903
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1157
904
  {
1158
905
    message::Table::Field pfield= table.field(fieldnr);
1159
906
 
1165
912
 
1166
913
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
1167
914
    {
1168
 
      char errmsg[100];
1169
 
      snprintf(errmsg, sizeof(errmsg),
1170
 
               _("ENUM column %s has greater than %d possible values"),
1171
 
               pfield.name().c_str(),
1172
 
               Field_enum::max_supported_elements);
1173
 
      errmsg[99]='\0';
 
915
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
1174
916
 
1175
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1176
 
      return ER_CORRUPT_TABLE_DEFINITION;
 
917
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
1177
918
    }
1178
919
 
1179
920
 
1216
957
  /* and read the fields */
1217
958
  interval_nr= 0;
1218
959
 
1219
 
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
 
960
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
1220
961
 
1221
962
  unsigned char* null_pos= getDefaultValues();
1222
963
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1223
964
 
1224
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
965
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1225
966
  {
1226
967
    message::Table::Field pfield= table.field(fieldnr);
1227
968
 
1247
988
        unireg_type= Field::TIMESTAMP_DN_FIELD;
1248
989
      }
1249
990
      else
1250
 
        assert(1); // Invalid update value.
 
991
      {
 
992
        assert(0); // Invalid update value.
 
993
        abort();
 
994
      }
1251
995
    }
1252
996
    else if (pfield.has_options() &&
1253
997
             pfield.options().has_update_expression() &&
1273
1017
 
1274
1018
    enum_field_types field_type;
1275
1019
 
1276
 
    field_type= proto_field_type_to_drizzle_type(pfield.type());
 
1020
    field_type= proto_field_type_to_drizzle_type(pfield);
1277
1021
 
1278
1022
    const CHARSET_INFO *charset= &my_charset_bin;
1279
1023
 
1318
1062
      {
1319
1063
        if (fo.scale() > DECIMAL_MAX_SCALE)
1320
1064
        {
1321
 
          local_error= 4;
 
1065
          local_error= ER_NOT_FORM_FILE;
1322
1066
 
1323
 
          return local_error;
 
1067
          return true;
1324
1068
        }
1325
1069
        decimals= static_cast<uint8_t>(fo.scale());
1326
1070
      }
1340
1084
    }
1341
1085
 
1342
1086
 
1343
 
    db_low_byte_first= true; //Cursor->low_byte_first();
1344
 
    blob_ptr_size= portable_sizeof_char_ptr;
1345
 
 
1346
1087
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1347
1088
 
 
1089
    // We set field_length in this loop.
1348
1090
    switch (field_type)
1349
1091
    {
1350
1092
    case DRIZZLE_TYPE_BLOB:
1376
1118
            decimals != NOT_FIXED_DEC)
1377
1119
        {
1378
1120
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1379
 
          local_error= 1;
1380
 
 
1381
 
          return local_error;
 
1121
          local_error= ER_M_BIGGER_THAN_D;
 
1122
          return true;
1382
1123
        }
1383
1124
        break;
1384
1125
      }
1386
1127
      {
1387
1128
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1388
1129
 
1389
 
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1130
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1390
1131
                                                     false);
1391
1132
        break;
1392
1133
      }
1393
 
    case DRIZZLE_TYPE_TIMESTAMP:
1394
1134
    case DRIZZLE_TYPE_DATETIME:
1395
1135
      field_length= DateTime::MAX_STRING_LENGTH;
1396
1136
      break;
1423
1163
      }
1424
1164
      break;
1425
1165
    case DRIZZLE_TYPE_LONGLONG:
1426
 
      field_length= MAX_BIGINT_WIDTH;
 
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();
1427
1185
      break;
1428
1186
    case DRIZZLE_TYPE_NULL:
1429
1187
      abort(); // Programming error
1430
1188
    }
1431
1189
 
1432
 
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1433
 
                                field_length,
1434
 
                                pfield.constraints().is_nullable(),
1435
 
                                null_pos,
1436
 
                                null_bit_pos,
1437
 
                                decimals,
1438
 
                                field_type,
1439
 
                                charset,
1440
 
                                (Field::utype) MTYP_TYPENR(unireg_type),
1441
 
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
1442
 
                                 &intervals[interval_nr++]
1443
 
                                 : (TYPELIB*) 0),
1444
 
                                getTableProto()->field(fieldnr).name().c_str());
1445
 
 
1446
 
    field[fieldnr]= f;
 
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
    }
1447
1236
 
1448
1237
    // This needs to go, we should be setting the "use" on the field so that
1449
1238
    // it does not reference the share/table.
1469
1258
      if (res != 0 && res != 3) /* @TODO Huh? */
1470
1259
      {
1471
1260
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1472
 
        local_error= 1;
 
1261
        local_error= ER_INVALID_DEFAULT;
1473
1262
 
1474
 
        return local_error;
 
1263
        return true;
1475
1264
      }
1476
1265
    }
1477
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1478
 
             (f->flags & NOT_NULL_FLAG))
 
1266
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1479
1267
    {
1480
1268
      f->set_notnull();
1481
1269
      f->store((int64_t) 1, true);
1489
1277
    f->setTable(NULL);
1490
1278
    f->orig_table= NULL;
1491
1279
 
1492
 
    f->field_index= fieldnr;
 
1280
    f->setPosition(fieldnr);
1493
1281
    f->comment= comment;
1494
 
    if (! default_value &&
1495
 
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
 
1282
    if (not default_value &&
 
1283
        not (f->unireg_check==Field::NEXT_NUMBER) &&
1496
1284
        (f->flags & NOT_NULL_FLAG) &&
1497
 
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
 
1285
        (not f->is_timestamp()))
1498
1286
    {
1499
1287
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1500
1288
    }
1501
1289
 
1502
1290
    if (f->unireg_check == Field::NEXT_NUMBER)
1503
 
      found_next_number_field= &(field[fieldnr]);
1504
 
 
1505
 
    if (timestamp_field == f)
1506
 
      timestamp_field_offset= fieldnr;
 
1291
      found_next_number_field= &(_fields[fieldnr]);
1507
1292
 
1508
1293
    if (use_hash) /* supposedly this never fails... but comments lie */
1509
1294
    {
1510
 
      const char *local_field_name= field[fieldnr]->field_name;
1511
 
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
 
1295
      const char *local_field_name= _fields[fieldnr]->field_name;
 
1296
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1512
1297
    }
1513
 
 
1514
1298
  }
1515
1299
 
1516
1300
  keyinfo= key_info;
1535
1319
    We need to set the unused bits to 1. If the number of bits is a multiple
1536
1320
    of 8 there are no unused bits.
1537
1321
  */
1538
 
 
1539
1322
  if (null_count & 7)
1540
1323
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1541
1324
 
1566
1349
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1567
1350
        {
1568
1351
          uint32_t fieldnr= key_part[i].fieldnr;
1569
 
          if (! fieldnr ||
1570
 
              field[fieldnr-1]->null_ptr ||
1571
 
              field[fieldnr-1]->key_length() != key_part[i].length)
 
1352
          if (not fieldnr ||
 
1353
              _fields[fieldnr-1]->null_ptr ||
 
1354
              _fields[fieldnr-1]->key_length() != key_part[i].length)
1572
1355
          {
1573
1356
            local_primary_key= MAX_KEY; // Can't be used
1574
1357
            break;
1583
1366
        {
1584
1367
          return ENOMEM;
1585
1368
        }
1586
 
        local_field= key_part->field= field[key_part->fieldnr-1];
 
1369
        local_field= key_part->field= _fields[key_part->fieldnr-1];
1587
1370
        key_part->type= local_field->key_type();
1588
1371
        if (local_field->null_ptr)
1589
1372
        {
1689
1472
                            &next_number_keypart)) < 0)
1690
1473
    {
1691
1474
      /* Wrong field definition */
1692
 
      local_error= 4;
 
1475
      local_error= ER_NOT_FORM_FILE;
1693
1476
 
1694
 
      return local_error;
 
1477
      return true;
1695
1478
    }
1696
1479
    else
1697
1480
    {
1705
1488
    blob_field.resize(blob_fields);
1706
1489
    uint32_t *save= &blob_field[0];
1707
1490
    uint32_t k= 0;
1708
 
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
 
1491
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1709
1492
    {
1710
1493
      if ((*iter)->flags & BLOB_FLAG)
1711
1494
        (*save++)= k;
1712
1495
    }
1713
1496
  }
1714
1497
 
1715
 
  db_low_byte_first= true; // @todo Question this.
1716
1498
  all_set.clear();
1717
 
  all_set.resize(fields);
 
1499
  all_set.resize(_field_size);
1718
1500
  all_set.set();
1719
1501
 
1720
 
  return local_error;
1721
 
}
1722
 
 
1723
 
int TableShare::parse_table_proto(Session& session, message::Table &table)
1724
 
{
1725
 
  int local_error= inner_parse_table_proto(session, table);
1726
 
 
1727
 
  if (not local_error)
1728
 
    return 0;
1729
 
 
1730
 
  error= local_error;
1731
 
  open_errno= errno;
1732
 
  errarg= 0;
1733
 
  open_table_error(local_error, open_errno, 0);
1734
 
 
1735
 
  return local_error;
1736
 
}
1737
 
 
 
1502
  return local_error != EE_OK;
 
1503
}
1738
1504
 
1739
1505
/*
1740
1506
  Read table definition from a binary / text based .frm cursor
1746
1512
 
1747
1513
  NOTES
1748
1514
  This function is called when the table definition is not cached in
1749
 
  table_def_cache
 
1515
  definition::Cache::singleton().getCache()
1750
1516
  The data is returned in 'share', which is alloced by
1751
1517
  alloc_table_share().. The code assumes that share is initialized.
1752
1518
 
1760
1526
  6    Unknown .frm version
1761
1527
*/
1762
1528
 
1763
 
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
 
1529
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1764
1530
{
1765
 
  int local_error;
1766
 
  bool error_given;
1767
 
 
1768
 
  local_error= 1;
1769
 
  error_given= 0;
1770
 
 
1771
 
  message::Table table;
1772
 
 
1773
 
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1774
 
 
1775
 
  if (local_error != EEXIST)
 
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())
1776
1536
  {
1777
 
    if (local_error > 0)
 
1537
    if (parse_table_proto(session, *table))
1778
1538
    {
1779
 
      errno= local_error;
1780
 
      local_error= 1;
 
1539
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1540
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1781
1541
    }
1782
1542
    else
1783
1543
    {
1784
 
      if (not table.IsInitialized())
1785
 
      {
1786
 
        local_error= 4;
1787
 
      }
 
1544
      setTableCategory(TABLE_CATEGORY_USER);
 
1545
      local_error= EE_OK;
1788
1546
    }
1789
 
    goto err_not_open;
1790
 
  }
1791
 
 
1792
 
  local_error= parse_table_proto(session, table);
1793
 
 
1794
 
  setTableCategory(TABLE_CATEGORY_USER);
1795
 
 
1796
 
err_not_open:
1797
 
  if (local_error && !error_given)
1798
 
  {
1799
 
    error= local_error;
1800
 
    open_table_error(error, (open_errno= errno), 0);
1801
 
  }
1802
 
 
1803
 
  return(error);
 
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);
1804
1560
}
1805
1561
 
1806
1562
 
1828
1584
  7    Table definition has changed in engine
1829
1585
*/
1830
1586
int TableShare::open_table_from_share(Session *session,
1831
 
                                      const TableIdentifier &identifier,
 
1587
                                      const identifier::Table &identifier,
1832
1588
                                      const char *alias,
1833
1589
                                      uint32_t db_stat, uint32_t ha_open_flags,
1834
1590
                                      Table &outparam)
1835
1591
{
1836
1592
  bool error_reported= false;
1837
 
  int ret= open_table_from_share_inner(session, alias, db_stat, ha_open_flags, outparam, error_reported);
 
1593
  int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1838
1594
 
1839
1595
  if (not ret)
1840
 
    ret= open_table_cursor_inner(session, identifier, alias, db_stat, ha_open_flags, outparam, error_reported);
 
1596
    ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1841
1597
 
1842
1598
  if (not ret)
1843
1599
    return ret;
1845
1601
  if (not error_reported)
1846
1602
    open_table_error(ret, errno, 0);
1847
1603
 
1848
 
  delete outparam.cursor;
 
1604
  boost::checked_delete(outparam.cursor);
1849
1605
  outparam.cursor= 0;                           // For easier error checking
1850
1606
  outparam.db_stat= 0;
1851
1607
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1852
 
  free((char*) outparam.alias);
 
1608
  outparam.clearAlias();
1853
1609
 
1854
1610
  return ret;
1855
1611
}
1856
1612
 
1857
1613
int TableShare::open_table_from_share_inner(Session *session,
1858
1614
                                            const char *alias,
1859
 
                                            uint32_t db_stat, uint32_t ,
1860
 
                                            Table &outparam,
1861
 
                                            bool &)
 
1615
                                            uint32_t db_stat,
 
1616
                                            Table &outparam)
1862
1617
{
1863
1618
  int local_error;
1864
1619
  uint32_t records;
1865
1620
  unsigned char *record= NULL;
1866
1621
  Field **field_ptr;
1867
1622
 
1868
 
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1869
 
  assert(session->lex->is_lex_started);
1870
 
 
1871
1623
  local_error= 1;
1872
1624
  outparam.resetTable(session, this, db_stat);
1873
1625
 
1874
 
  if (not (outparam.alias= strdup(alias)))
1875
 
    return local_error;
 
1626
  outparam.setAlias(alias);
1876
1627
 
1877
1628
  /* Allocate Cursor */
1878
 
  if (not (outparam.cursor= db_type()->getCursor(*this)))
 
1629
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
1879
1630
    return local_error;
1880
1631
 
1881
1632
  local_error= 4;
1902
1653
      outparam.record[1]= outparam.getInsertRecord();   // Safety
1903
1654
  }
1904
1655
 
1905
 
#ifdef HAVE_purify
 
1656
#ifdef HAVE_VALGRIND
1906
1657
  /*
1907
1658
    We need this because when we read var-length rows, we are not updating
1908
1659
    bytes after end of varchar
1920
1671
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1921
1672
  }
1922
1673
 
1923
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
 
1674
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
1924
1675
  {
1925
1676
    return local_error;
1926
1677
  }
1932
1683
  outparam.null_flags= (unsigned char*) record+1;
1933
1684
 
1934
1685
  /* Setup copy of fields from share, but use the right alias and record */
1935
 
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
 
1686
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1936
1687
  {
1937
 
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1688
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1938
1689
      return local_error;
1939
1690
  }
1940
1691
  (*field_ptr)= 0;                              // End marker
1943
1694
    outparam.found_next_number_field=
1944
1695
      outparam.getField(positionFields(found_next_number_field));
1945
1696
  if (timestamp_field)
1946
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1947
 
 
 
1697
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1948
1698
 
1949
1699
  /* Fix key->name and key_part->field */
1950
1700
  if (key_parts)
1993
1743
 
1994
1744
  /* Allocate bitmaps */
1995
1745
 
1996
 
  outparam.def_read_set.resize(fields);
1997
 
  outparam.def_write_set.resize(fields);
1998
 
  outparam.tmp_set.resize(fields);
 
1746
  outparam.def_read_set.resize(_field_size);
 
1747
  outparam.def_write_set.resize(_field_size);
 
1748
  outparam.tmp_set.resize(_field_size);
1999
1749
  outparam.default_column_bitmaps();
2000
1750
 
2001
1751
  return 0;
2002
1752
}
2003
1753
 
2004
 
int TableShare::open_table_cursor_inner(Session *,
2005
 
                                        const TableIdentifier &identifier,
2006
 
                                        const char *,
 
1754
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
2007
1755
                                        uint32_t db_stat, uint32_t ha_open_flags,
2008
1756
                                        Table &outparam,
2009
1757
                                        bool &error_reported)
2015
1763
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
2016
1764
    int ha_err;
2017
1765
 
2018
 
    if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam,
2019
 
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
2020
 
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
 
1766
    if ((ha_err= (outparam.cursor->ha_open(identifier,
 
1767
                                           (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
1768
                                           (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
2021
1769
    {
2022
1770
      switch (ha_err)
2023
1771
      {
2054
1802
/* error message when opening a form cursor */
2055
1803
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
2056
1804
{
2057
 
  int err_no;
2058
1805
  char buff[FN_REFLEN];
2059
1806
  myf errortype= ME_ERROR+ME_WAITTANG;
2060
1807
 
2063
1810
  case 1:
2064
1811
    if (db_errno == ENOENT)
2065
1812
    {
2066
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
 
1813
      identifier::Table identifier(db.str, table_name.str);
 
1814
      my_error(ER_TABLE_UNKNOWN, identifier);
2067
1815
    }
2068
1816
    else
2069
1817
    {
2074
1822
    break;
2075
1823
  case 2:
2076
1824
    {
 
1825
      drizzled::error_t err_no;
 
1826
 
2077
1827
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2078
1828
        ER_FILE_USED : ER_CANT_OPEN_FILE;
 
1829
 
2079
1830
      my_error(err_no, errortype, normalized_path.str, db_errno);
2080
1831
      break;
2081
1832
    }
2111
1862
  return;
2112
1863
} /* open_table_error */
2113
1864
 
2114
 
Field *TableShare::make_field(unsigned char *ptr,
 
1865
Field *TableShare::make_field(const message::Table::Field &pfield,
 
1866
                              unsigned char *ptr,
2115
1867
                              uint32_t field_length,
2116
1868
                              bool is_nullable,
2117
1869
                              unsigned char *null_pos,
2123
1875
                              TYPELIB *interval,
2124
1876
                              const char *field_name)
2125
1877
{
 
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
{
2126
1907
  if (! is_nullable)
2127
1908
  {
2128
1909
    null_pos=0;
2133
1914
    null_bit= ((unsigned char) 1) << null_bit;
2134
1915
  }
2135
1916
 
2136
 
  switch (field_type) 
2137
 
  {
2138
 
  case DRIZZLE_TYPE_DATE:
2139
 
  case DRIZZLE_TYPE_DATETIME:
2140
 
  case DRIZZLE_TYPE_TIMESTAMP:
2141
 
    field_charset= &my_charset_bin;
2142
 
  default: break;
2143
 
  }
2144
 
 
2145
1917
  switch (field_type)
2146
1918
  {
2147
1919
  case DRIZZLE_TYPE_ENUM:
2148
1920
    return new (&mem_root) Field_enum(ptr,
2149
 
                                 field_length,
2150
 
                                 null_pos,
2151
 
                                 null_bit,
2152
 
                                 field_name,
2153
 
                                 interval,
2154
 
                                 field_charset);
 
1921
                                      field_length,
 
1922
                                      null_pos,
 
1923
                                      null_bit,
 
1924
                                      field_name,
 
1925
                                      interval,
 
1926
                                      field_charset);
2155
1927
  case DRIZZLE_TYPE_VARCHAR:
2156
1928
    setVariableWidth();
2157
1929
    return new (&mem_root) Field_varstring(ptr,field_length,
2158
 
                                      HA_VARCHAR_PACKLENGTH(field_length),
 
1930
                                      ha_varchar_packlength(field_length),
2159
1931
                                      null_pos,null_bit,
2160
1932
                                      field_name,
2161
1933
                                      field_charset);
2162
1934
  case DRIZZLE_TYPE_BLOB:
2163
1935
    return new (&mem_root) Field_blob(ptr,
2164
 
                                 null_pos,
2165
 
                                 null_bit,
2166
 
                                 field_name,
2167
 
                                 this,
2168
 
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
2169
 
                                 field_charset);
 
1936
                                      null_pos,
 
1937
                                      null_bit,
 
1938
                                      field_name,
 
1939
                                      this,
 
1940
                                      field_charset);
2170
1941
  case DRIZZLE_TYPE_DECIMAL:
2171
1942
    return new (&mem_root) Field_decimal(ptr,
2172
 
                                    field_length,
2173
 
                                    null_pos,
2174
 
                                    null_bit,
2175
 
                                    unireg_check,
2176
 
                                    field_name,
2177
 
                                    decimals,
2178
 
                                    false,
2179
 
                                    false /* is_unsigned */);
 
1943
                                         field_length,
 
1944
                                         null_pos,
 
1945
                                         null_bit,
 
1946
                                         unireg_check,
 
1947
                                         field_name,
 
1948
                                         decimals);
2180
1949
  case DRIZZLE_TYPE_DOUBLE:
2181
1950
    return new (&mem_root) Field_double(ptr,
2182
1951
                                   field_length,
2187
1956
                                   decimals,
2188
1957
                                   false,
2189
1958
                                   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);
2190
1972
  case DRIZZLE_TYPE_LONG:
2191
 
    return new (&mem_root) Field_long(ptr,
2192
 
                                 field_length,
2193
 
                                 null_pos,
2194
 
                                 null_bit,
2195
 
                                 unireg_check,
2196
 
                                 field_name,
2197
 
                                 false,
2198
 
                                 false /* is_unsigned */);
 
1973
    return new (&mem_root) field::Int32(ptr,
 
1974
                                        field_length,
 
1975
                                        null_pos,
 
1976
                                        null_bit,
 
1977
                                        unireg_check,
 
1978
                                        field_name);
2199
1979
  case DRIZZLE_TYPE_LONGLONG:
2200
 
    return new (&mem_root) Field_int64_t(ptr,
2201
 
                                    field_length,
2202
 
                                    null_pos,
2203
 
                                    null_bit,
2204
 
                                    unireg_check,
2205
 
                                    field_name,
2206
 
                                    false,
2207
 
                                    false /* is_unsigned */);
 
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);
2208
2005
  case DRIZZLE_TYPE_TIMESTAMP:
2209
 
    return new (&mem_root) Field_timestamp(ptr,
2210
 
                                      field_length,
2211
 
                                      null_pos,
2212
 
                                      null_bit,
2213
 
                                      unireg_check,
2214
 
                                      field_name,
2215
 
                                      this,
2216
 
                                      field_charset);
 
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);
2217
2018
  case DRIZZLE_TYPE_DATE:
2218
2019
    return new (&mem_root) Field_date(ptr,
2219
2020
                                 null_pos,
2220
2021
                                 null_bit,
2221
 
                                 field_name,
2222
 
                                 field_charset);
 
2022
                                 field_name);
2223
2023
  case DRIZZLE_TYPE_DATETIME:
2224
2024
    return new (&mem_root) Field_datetime(ptr,
2225
2025
                                     null_pos,
2226
2026
                                     null_bit,
2227
 
                                     field_name,
2228
 
                                     field_charset);
 
2027
                                     field_name);
2229
2028
  case DRIZZLE_TYPE_NULL:
2230
2029
    return new (&mem_root) Field_null(ptr,
2231
 
                                 field_length,
2232
 
                                 field_name,
2233
 
                                 field_charset);
2234
 
  default: // Impossible (Wrong version)
2235
 
    break;
 
2030
                                      field_length,
 
2031
                                      field_name);
2236
2032
  }
2237
 
  return 0;
 
2033
  assert(0);
 
2034
  abort();
 
2035
}
 
2036
 
 
2037
void TableShare::refreshVersion()
 
2038
{
 
2039
  version= refresh_version;
2238
2040
}
2239
2041
 
2240
2042