~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.cc

  • Committer: Brian Aker
  • Date: 2010-07-28 23:07:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1671.
  • Revision ID: brian@gaz-20100728230742-idji8pjd3trphd1a
Fix up a few additional cases around case insensitive usage for
unordered_map. This also places the code in util/string.h behind an
additional namespace of "util"

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/session.h"
 
48
 
 
49
#include "drizzled/charset.h"
 
50
#include "drizzled/internal/m_string.h"
 
51
#include "drizzled/internal/my_sys.h"
 
52
 
 
53
#include "drizzled/item/string.h"
 
54
#include "drizzled/item/int.h"
 
55
#include "drizzled/item/decimal.h"
 
56
#include "drizzled/item/float.h"
 
57
#include "drizzled/item/null.h"
 
58
#include "drizzled/temporal.h"
 
59
 
 
60
#include "drizzled/field.h"
 
61
#include "drizzled/field/str.h"
 
62
#include "drizzled/field/num.h"
 
63
#include "drizzled/field/blob.h"
 
64
#include "drizzled/field/enum.h"
 
65
#include "drizzled/field/null.h"
 
66
#include "drizzled/field/date.h"
 
67
#include "drizzled/field/decimal.h"
 
68
#include "drizzled/field/real.h"
 
69
#include "drizzled/field/double.h"
 
70
#include "drizzled/field/long.h"
 
71
#include "drizzled/field/int64_t.h"
 
72
#include "drizzled/field/num.h"
 
73
#include "drizzled/field/timestamp.h"
 
74
#include "drizzled/field/datetime.h"
 
75
#include "drizzled/field/varstring.h"
90
76
 
91
77
using namespace std;
92
78
 
94
80
{
95
81
 
96
82
extern size_t table_def_size;
97
 
 
98
 
 
99
 
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
100
 
{
101
 
  switch(field.type())
 
83
static TableDefinitionCache table_def_cache;
 
84
static pthread_mutex_t LOCK_table_share;
 
85
bool table_def_inited= false;
 
86
 
 
87
/*****************************************************************************
 
88
  Functions to handle table definition cach (TableShare)
 
89
 *****************************************************************************/
 
90
 
 
91
 
 
92
void TableShare::cacheStart(void)
 
93
{
 
94
  pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
 
95
  table_def_inited= true;
 
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
void TableShare::cacheStop(void)
 
106
{
 
107
  if (table_def_inited)
 
108
  {
 
109
    table_def_inited= false;
 
110
    pthread_mutex_destroy(&LOCK_table_share);
 
111
  }
 
112
}
 
113
 
 
114
 
 
115
/**
 
116
 * @TODO: This should return size_t
 
117
 */
 
118
uint32_t cached_table_definitions(void)
 
119
{
 
120
  return static_cast<uint32_t>(table_def_cache.size());
 
121
}
 
122
 
 
123
 
 
124
/*
 
125
  Mark that we are not using table share anymore.
 
126
 
 
127
  SYNOPSIS
 
128
  release()
 
129
  share         Table share
 
130
 
 
131
  IMPLEMENTATION
 
132
  If ref_count goes to zero and (we have done a refresh or if we have
 
133
  already too many open table shares) then delete the definition.
 
134
*/
 
135
 
 
136
void TableShare::release(TableShare *share)
 
137
{
 
138
  bool to_be_deleted= false;
 
139
  safe_mutex_assert_owner(&LOCK_open);
 
140
 
 
141
  pthread_mutex_lock(&share->mutex);
 
142
  if (!--share->ref_count)
 
143
  {
 
144
    to_be_deleted= true;
 
145
  }
 
146
 
 
147
  if (to_be_deleted)
 
148
  {
 
149
    TableIdentifier identifier(share->getSchemaName(), share->getTableName());
 
150
    plugin::EventObserver::deregisterTableEvents(*share);
 
151
   
 
152
    TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
153
    if (iter != table_def_cache.end())
 
154
    {
 
155
      table_def_cache.erase(iter);
 
156
      delete share;
 
157
    }
 
158
    return;
 
159
  }
 
160
  pthread_mutex_unlock(&share->mutex);
 
161
}
 
162
 
 
163
void TableShare::release(TableIdentifier &identifier)
 
164
{
 
165
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
166
  if (iter != table_def_cache.end())
 
167
  {
 
168
    TableShare *share= (*iter).second;
 
169
    share->version= 0;                          // Mark for delete
 
170
    if (share->ref_count == 0)
 
171
    {
 
172
      pthread_mutex_lock(&share->mutex);
 
173
      plugin::EventObserver::deregisterTableEvents(*share);
 
174
      table_def_cache.erase(identifier.getKey());
 
175
      delete share;
 
176
    }
 
177
  }
 
178
}
 
179
 
 
180
 
 
181
static TableShare *foundTableShare(TableShare *share)
 
182
{
 
183
  /*
 
184
    We found an existing table definition. Return it if we didn't get
 
185
    an error when reading the table definition from file.
 
186
  */
 
187
 
 
188
  /* We must do a lock to ensure that the structure is initialized */
 
189
  (void) pthread_mutex_lock(&share->mutex);
 
190
  if (share->error)
 
191
  {
 
192
    /* Table definition contained an error */
 
193
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
194
    (void) pthread_mutex_unlock(&share->mutex);
 
195
 
 
196
    return NULL;
 
197
  }
 
198
 
 
199
  share->incrementTableCount();
 
200
  (void) pthread_mutex_unlock(&share->mutex);
 
201
 
 
202
  return share;
 
203
}
 
204
 
 
205
/*
 
206
  Get TableShare for a table.
 
207
 
 
208
  get_table_share()
 
209
  session                       Thread handle
 
210
  table_list            Table that should be opened
 
211
  key                   Table cache key
 
212
  key_length            Length of key
 
213
  error                 out: Error code from open_table_def()
 
214
 
 
215
  IMPLEMENTATION
 
216
  Get a table definition from the table definition cache.
 
217
  If it doesn't exist, create a new from the table definition file.
 
218
 
 
219
  NOTES
 
220
  We must have wrlock on LOCK_open when we come here
 
221
  (To be changed later)
 
222
 
 
223
  RETURN
 
224
  0  Error
 
225
#  Share for table
 
226
*/
 
227
 
 
228
TableShare *TableShare::getShareCreate(Session *session, 
 
229
                                       TableIdentifier &identifier,
 
230
                                       int *error)
 
231
{
 
232
  TableShare *share= NULL;
 
233
 
 
234
  *error= 0;
 
235
 
 
236
  /* Read table definition from cache */
 
237
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
238
  if (iter != table_def_cache.end())
 
239
  {
 
240
    share= (*iter).second;
 
241
    return foundTableShare(share);
 
242
  }
 
243
 
 
244
  if (not (share= new TableShare(message::Table::STANDARD, identifier)))
 
245
  {
 
246
    return NULL;
 
247
  }
 
248
 
 
249
  /*
 
250
    Lock mutex to be able to read table definition from file without
 
251
    conflicts
 
252
  */
 
253
  (void) pthread_mutex_lock(&share->mutex);
 
254
 
 
255
  /**
 
256
   * @TODO: we need to eject something if we exceed table_def_size
 
257
 */
 
258
  pair<TableDefinitionCache::iterator, bool> ret=
 
259
    table_def_cache.insert(make_pair(identifier.getKey(), share));
 
260
  if (ret.second == false)
 
261
  {
 
262
    delete share;
 
263
 
 
264
    return NULL;
 
265
  }
 
266
 
 
267
  if (share->open_table_def(*session, identifier))
 
268
  {
 
269
    *error= share->error;
 
270
    table_def_cache.erase(identifier.getKey());
 
271
    delete share;
 
272
 
 
273
    return NULL;
 
274
  }
 
275
  share->ref_count++;                           // Mark in use
 
276
  
 
277
  plugin::EventObserver::registerTableEvents(*share);
 
278
  
 
279
  (void) pthread_mutex_unlock(&share->mutex);
 
280
 
 
281
  return share;
 
282
}
 
283
 
 
284
 
 
285
/*
 
286
  Check if table definition exits in cache
 
287
 
 
288
  SYNOPSIS
 
289
  get_cached_table_share()
 
290
  db                    Database name
 
291
  table_name            Table name
 
292
 
 
293
  RETURN
 
294
  0  Not cached
 
295
#  TableShare for table
 
296
*/
 
297
TableShare *TableShare::getShare(TableIdentifier &identifier)
 
298
{
 
299
  safe_mutex_assert_owner(&LOCK_open);
 
300
 
 
301
  TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
 
302
  if (iter != table_def_cache.end())
 
303
  {
 
304
    return (*iter).second;
 
305
  }
 
306
  else
 
307
  {
 
308
    return NULL;
 
309
  }
 
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)
102
317
  {
103
318
  case message::Table::Field::INTEGER:
104
 
    return DRIZZLE_TYPE_LONG;
105
 
 
 
319
    field_type= DRIZZLE_TYPE_LONG;
 
320
    break;
106
321
  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
 
 
 
322
    field_type= DRIZZLE_TYPE_DOUBLE;
 
323
    break;
 
324
  case message::Table::Field::TIMESTAMP:
 
325
    field_type= DRIZZLE_TYPE_TIMESTAMP;
 
326
    break;
115
327
  case message::Table::Field::BIGINT:
116
 
    return DRIZZLE_TYPE_LONGLONG;
117
 
 
 
328
    field_type= DRIZZLE_TYPE_LONGLONG;
 
329
    break;
118
330
  case message::Table::Field::DATETIME:
119
 
    return DRIZZLE_TYPE_DATETIME;
120
 
 
 
331
    field_type= DRIZZLE_TYPE_DATETIME;
 
332
    break;
121
333
  case message::Table::Field::DATE:
122
 
    return DRIZZLE_TYPE_DATE;
123
 
 
 
334
    field_type= DRIZZLE_TYPE_DATE;
 
335
    break;
124
336
  case message::Table::Field::VARCHAR:
125
 
    return DRIZZLE_TYPE_VARCHAR;
126
 
 
 
337
    field_type= DRIZZLE_TYPE_VARCHAR;
 
338
    break;
127
339
  case message::Table::Field::DECIMAL:
128
 
    return DRIZZLE_TYPE_DECIMAL;
129
 
 
 
340
    field_type= DRIZZLE_TYPE_DECIMAL;
 
341
    break;
130
342
  case message::Table::Field::ENUM:
131
 
    return DRIZZLE_TYPE_ENUM;
132
 
 
 
343
    field_type= DRIZZLE_TYPE_ENUM;
 
344
    break;
133
345
  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;
 
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);
144
351
  }
145
352
 
146
 
  abort();
 
353
  return field_type;
147
354
}
148
355
 
149
356
static Item *default_value_item(enum_field_types field_type,
174
381
                                 default_value->length());
175
382
    break;
176
383
  case DRIZZLE_TYPE_NULL:
177
 
    assert(0);
178
 
    abort();
 
384
    assert(false);
179
385
  case DRIZZLE_TYPE_TIMESTAMP:
180
386
  case DRIZZLE_TYPE_DATETIME:
181
 
  case DRIZZLE_TYPE_TIME:
182
387
  case DRIZZLE_TYPE_DATE:
 
388
    if (default_value->compare("NOW()") == 0)
 
389
      break;
183
390
  case DRIZZLE_TYPE_ENUM:
184
 
  case DRIZZLE_TYPE_UUID:
185
 
  case DRIZZLE_TYPE_MICROTIME:
186
 
  case DRIZZLE_TYPE_BOOLEAN:
187
391
    default_item= new Item_string(default_value->c_str(),
188
392
                                  default_value->length(),
189
393
                                  system_charset_info);
222
426
 */
223
427
bool TableShare::fieldInPrimaryKey(Field *in_field) const
224
428
{
225
 
  assert(getTableMessage());
 
429
  assert(table_proto != NULL);
226
430
 
227
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
431
  size_t num_indexes= table_proto->indexes_size();
228
432
 
229
433
  for (size_t x= 0; x < num_indexes; ++x)
230
434
  {
231
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
435
    const message::Table::Index &index= table_proto->indexes(x);
232
436
    if (index.is_primary())
233
437
    {
234
438
      size_t num_parts= index.index_part_size();
235
439
      for (size_t y= 0; y < num_parts; ++y)
236
440
      {
237
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
441
        if (index.index_part(y).fieldnr() == in_field->field_index)
238
442
          return true;
239
443
      }
240
444
    }
242
446
  return false;
243
447
}
244
448
 
245
 
TableShare::TableShare(const identifier::Table::Type type_arg) :
 
449
const TableDefinitionCache &TableShare::getCache()
 
450
{
 
451
  return table_def_cache;
 
452
}
 
453
 
 
454
TableShare::TableShare(TableIdentifier::Type type_arg) :
246
455
  table_category(TABLE_UNKNOWN_CATEGORY),
 
456
  open_count(0),
247
457
  found_next_number_field(NULL),
248
458
  timestamp_field(NULL),
249
459
  key_info(NULL),
250
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
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
  blob_field(NULL),
256
461
  block_size(0),
257
462
  version(0),
258
463
  timestamp_offset(0),
259
464
  reclength(0),
260
465
  stored_rec_length(0),
 
466
  row_type(ROW_TYPE_DEFAULT),
261
467
  max_rows(0),
262
 
  _table_message(NULL),
 
468
  table_proto(NULL),
263
469
  storage_engine(NULL),
264
470
  tmp_table(type_arg),
265
 
  _ref_count(0),
 
471
  ref_count(0),
266
472
  null_bytes(0),
267
473
  last_null_bit_pos(0),
268
 
  _field_size(0),
 
474
  fields(0),
269
475
  rec_buff_length(0),
270
476
  keys(0),
271
477
  key_parts(0),
275
481
  uniques(0),
276
482
  null_fields(0),
277
483
  blob_fields(0),
278
 
  has_variable_width(false),
 
484
  timestamp_field_offset(0),
 
485
  varchar_fields(0),
279
486
  db_create_options(0),
280
487
  db_options_in_use(0),
281
488
  db_record_offset(0),
287
494
  error(0),
288
495
  open_errno(0),
289
496
  errarg(0),
290
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
497
  column_bitmap_size(0),
 
498
  blob_ptr_size(0),
291
499
  db_low_byte_first(false),
 
500
  name_lock(false),
 
501
  replace_with_name_lock(false),
 
502
  waiting_on_cond(false),
292
503
  keys_in_use(0),
293
 
  keys_for_keyread(0)
 
504
  keys_for_keyread(0),
 
505
  event_observers(NULL),
 
506
  newed(true)
294
507
{
 
508
 
 
509
  table_charset= 0;
 
510
  memset(&db, 0, sizeof(LEX_STRING));
 
511
  memset(&table_name, 0, sizeof(LEX_STRING));
 
512
  memset(&path, 0, sizeof(LEX_STRING));
 
513
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
514
 
295
515
  if (type_arg == message::Table::INTERNAL)
296
516
  {
297
 
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
298
 
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
 
517
    TableIdentifier::build_tmptable_filename(private_key_for_cache);
 
518
    init(&private_key_for_cache[0], &private_key_for_cache[0]);
299
519
  }
300
520
  else
301
521
  {
303
523
  }
304
524
}
305
525
 
306
 
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
 
526
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
307
527
  table_category(TABLE_UNKNOWN_CATEGORY),
 
528
  open_count(0),
308
529
  found_next_number_field(NULL),
309
530
  timestamp_field(NULL),
310
531
  key_info(NULL),
311
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
312
 
  table_charset(0),
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),
 
532
  blob_field(NULL),
318
533
  block_size(0),
319
534
  version(0),
320
535
  timestamp_offset(0),
321
536
  reclength(0),
322
537
  stored_rec_length(0),
 
538
  row_type(ROW_TYPE_DEFAULT),
323
539
  max_rows(0),
324
 
  _table_message(NULL),
 
540
  table_proto(NULL),
325
541
  storage_engine(NULL),
326
542
  tmp_table(message::Table::INTERNAL),
327
 
  _ref_count(0),
 
543
  ref_count(0),
328
544
  null_bytes(0),
329
545
  last_null_bit_pos(0),
330
 
  _field_size(0),
 
546
  fields(0),
331
547
  rec_buff_length(0),
332
548
  keys(0),
333
549
  key_parts(0),
337
553
  uniques(0),
338
554
  null_fields(0),
339
555
  blob_fields(0),
340
 
  has_variable_width(false),
 
556
  timestamp_field_offset(0),
 
557
  varchar_fields(0),
341
558
  db_create_options(0),
342
559
  db_options_in_use(0),
343
560
  db_record_offset(0),
349
566
  error(0),
350
567
  open_errno(0),
351
568
  errarg(0),
352
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
569
  column_bitmap_size(0),
 
570
  blob_ptr_size(0),
353
571
  db_low_byte_first(false),
 
572
  name_lock(false),
 
573
  replace_with_name_lock(false),
 
574
  waiting_on_cond(false),
354
575
  keys_in_use(0),
355
 
  keys_for_keyread(0)
 
576
  keys_for_keyread(0),
 
577
  event_observers(NULL),
 
578
  newed(true)
356
579
{
357
580
  assert(identifier.getKey() == key);
358
581
 
 
582
  table_charset= 0;
 
583
  memset(&path, 0, sizeof(LEX_STRING));
 
584
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
585
 
359
586
  private_key_for_cache= key;
360
587
 
 
588
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
361
589
  table_category=         TABLE_CATEGORY_TEMPORARY;
362
590
  tmp_table=              message::Table::INTERNAL;
363
591
 
364
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
365
 
  db.length= strlen(private_key_for_cache.vector());
 
592
  db.str= &private_key_for_cache[0];
 
593
  db.length= strlen(&private_key_for_cache[0]);
366
594
 
367
 
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
 
595
  table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
368
596
  table_name.length= strlen(table_name.str);
369
597
  path.str= (char *)"";
370
598
  normalized_path.str= path.str;
371
599
  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
 
 
 
600
  assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
377
601
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
378
602
}
379
603
 
380
604
 
381
 
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
 
605
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
382
606
  table_category(TABLE_UNKNOWN_CATEGORY),
 
607
  open_count(0),
383
608
  found_next_number_field(NULL),
384
609
  timestamp_field(NULL),
385
610
  key_info(NULL),
386
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
387
 
  table_charset(0),
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),
 
611
  blob_field(NULL),
393
612
  block_size(0),
394
613
  version(0),
395
614
  timestamp_offset(0),
396
615
  reclength(0),
397
616
  stored_rec_length(0),
 
617
  row_type(ROW_TYPE_DEFAULT),
398
618
  max_rows(0),
399
 
  _table_message(NULL),
 
619
  table_proto(NULL),
400
620
  storage_engine(NULL),
401
621
  tmp_table(identifier.getType()),
402
 
  _ref_count(0),
 
622
  ref_count(0),
403
623
  null_bytes(0),
404
624
  last_null_bit_pos(0),
405
 
  _field_size(0),
 
625
  fields(0),
406
626
  rec_buff_length(0),
407
627
  keys(0),
408
628
  key_parts(0),
412
632
  uniques(0),
413
633
  null_fields(0),
414
634
  blob_fields(0),
415
 
  has_variable_width(false),
 
635
  timestamp_field_offset(0),
 
636
  varchar_fields(0),
416
637
  db_create_options(0),
417
638
  db_options_in_use(0),
418
639
  db_record_offset(0),
424
645
  error(0),
425
646
  open_errno(0),
426
647
  errarg(0),
427
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
648
  column_bitmap_size(0),
 
649
  blob_ptr_size(0),
428
650
  db_low_byte_first(false),
 
651
  name_lock(false),
 
652
  replace_with_name_lock(false),
 
653
  waiting_on_cond(false),
429
654
  keys_in_use(0),
430
 
  keys_for_keyread(0)
 
655
  keys_for_keyread(0),
 
656
  event_observers(NULL),
 
657
  newed(true)
431
658
{
 
659
  table_charset= 0;
 
660
  memset(&db, 0, sizeof(LEX_STRING));
 
661
  memset(&table_name, 0, sizeof(LEX_STRING));
 
662
  memset(&path, 0, sizeof(LEX_STRING));
 
663
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
664
 
432
665
  private_key_for_cache= identifier.getKey();
433
666
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
434
667
  private_normalized_path.resize(identifier.getPath().size() + 1);
435
668
  memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
436
669
 
437
670
  {
 
671
    memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
438
672
    table_category=         TABLE_CATEGORY_TEMPORARY;
439
673
    tmp_table=              message::Table::INTERNAL;
440
 
    db.str= const_cast<char *>(private_key_for_cache.vector());
441
 
    db.length= strlen(private_key_for_cache.vector());
 
674
    db.str= &private_key_for_cache[0];
 
675
    db.length= strlen(&private_key_for_cache[0]);
442
676
    table_name.str= db.str + 1;
443
677
    table_name.length= strlen(table_name.str);
444
678
    path.str= &private_normalized_path[0];
451
685
/*
452
686
  Used for shares that will go into the cache.
453
687
*/
454
 
TableShare::TableShare(const identifier::Table::Type type_arg,
455
 
                       const identifier::Table &identifier,
 
688
TableShare::TableShare(TableIdentifier::Type type_arg,
 
689
                       TableIdentifier &identifier,
456
690
                       char *path_arg,
457
691
                       uint32_t path_length_arg) :
458
692
  table_category(TABLE_UNKNOWN_CATEGORY),
 
693
  open_count(0),
459
694
  found_next_number_field(NULL),
460
695
  timestamp_field(NULL),
461
696
  key_info(NULL),
462
 
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
463
 
  table_charset(0),
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),
 
697
  blob_field(NULL),
469
698
  block_size(0),
470
699
  version(0),
471
700
  timestamp_offset(0),
472
701
  reclength(0),
473
702
  stored_rec_length(0),
 
703
  row_type(ROW_TYPE_DEFAULT),
474
704
  max_rows(0),
475
 
  _table_message(NULL),
 
705
  table_proto(NULL),
476
706
  storage_engine(NULL),
477
707
  tmp_table(type_arg),
478
 
  _ref_count(0),
 
708
  ref_count(0),
479
709
  null_bytes(0),
480
710
  last_null_bit_pos(0),
481
 
  _field_size(0),
 
711
  fields(0),
482
712
  rec_buff_length(0),
483
713
  keys(0),
484
714
  key_parts(0),
488
718
  uniques(0),
489
719
  null_fields(0),
490
720
  blob_fields(0),
491
 
  has_variable_width(false),
 
721
  timestamp_field_offset(0),
 
722
  varchar_fields(0),
492
723
  db_create_options(0),
493
724
  db_options_in_use(0),
494
725
  db_record_offset(0),
500
731
  error(0),
501
732
  open_errno(0),
502
733
  errarg(0),
503
 
  blob_ptr_size(portable_sizeof_char_ptr),
 
734
  column_bitmap_size(0),
 
735
  blob_ptr_size(0),
504
736
  db_low_byte_first(false),
 
737
  name_lock(false),
 
738
  replace_with_name_lock(false),
 
739
  waiting_on_cond(false),
505
740
  keys_in_use(0),
506
 
  keys_for_keyread(0)
 
741
  keys_for_keyread(0),
 
742
  event_observers(NULL),
 
743
  newed(true)
507
744
{
 
745
  table_charset= 0;
 
746
  memset(&db, 0, sizeof(LEX_STRING));
 
747
  memset(&table_name, 0, sizeof(LEX_STRING));
 
748
  memset(&path, 0, sizeof(LEX_STRING));
 
749
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
750
 
 
751
  mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
508
752
  char *path_buff;
509
753
  std::string _path;
510
754
 
513
757
    Let us use the fact that the key is "db/0/table_name/0" + optional
514
758
    part for temporary tables.
515
759
  */
516
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
760
  db.str= &private_key_for_cache[0];
517
761
  db.length=         strlen(db.str);
518
762
  table_name.str=    db.str + db.length + 1;
519
763
  table_name.length= strlen(table_name.str);
524
768
  }
525
769
  else
526
770
  {
527
 
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
 
771
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
528
772
  }
529
773
 
530
 
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
 
774
  if (mem_root.multi_alloc_root(0,
 
775
                                &path_buff, _path.length() + 1,
 
776
                                NULL))
531
777
  {
532
778
    setPath(path_buff, _path.length());
533
779
    strcpy(path_buff, _path.c_str());
534
780
    setNormalizedPath(path_buff, _path.length());
535
781
 
536
782
    version= refresh_version;
 
783
 
 
784
    pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
 
785
    pthread_cond_init(&cond, NULL);
537
786
  }
538
787
  else
539
788
  {
540
789
    assert(0); // We should throw here.
541
 
    abort();
542
790
  }
 
791
 
 
792
  newed= true;
543
793
}
544
794
 
545
795
void TableShare::init(const char *new_table_name,
546
796
                      const char *new_path)
547
797
{
548
798
 
 
799
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
549
800
  table_category=         TABLE_CATEGORY_TEMPORARY;
550
801
  tmp_table=              message::Table::INTERNAL;
551
802
  db.str= (char *)"";
559
810
 
560
811
TableShare::~TableShare() 
561
812
{
 
813
  assert(ref_count == 0);
 
814
 
 
815
  /*
 
816
    If someone is waiting for this to be deleted, inform it about this.
 
817
    Don't do a delete until we know that no one is refering to this anymore.
 
818
  */
 
819
  if (tmp_table == message::Table::STANDARD)
 
820
  {
 
821
    /* share->mutex is locked in release_table_share() */
 
822
    while (waiting_on_cond)
 
823
    {
 
824
      pthread_cond_broadcast(&cond);
 
825
      pthread_cond_wait(&cond, &mutex);
 
826
    }
 
827
    /* No thread refers to this anymore */
 
828
    pthread_mutex_unlock(&mutex);
 
829
    pthread_mutex_destroy(&mutex);
 
830
    pthread_cond_destroy(&cond);
 
831
  }
 
832
 
562
833
  storage_engine= NULL;
563
834
 
 
835
  delete table_proto;
 
836
  table_proto= NULL;
 
837
 
564
838
  mem_root.free_root(MYF(0));                 // Free's share
565
839
}
566
840
 
567
 
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
 
841
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
568
842
{
 
843
  private_key_for_cache.clear();
569
844
  private_key_for_cache= identifier_arg.getKey();
570
845
 
571
846
  /*
572
847
    Let us use the fact that the key is "db/0/table_name/0" + optional
573
848
    part for temporary tables.
574
849
  */
575
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
 
850
  db.str= &private_key_for_cache[0];
576
851
  db.length=         strlen(db.str);
577
852
  table_name.str=    db.str + db.length + 1;
578
853
  table_name.length= strlen(table_name.str);
579
854
 
580
 
  getTableMessage()->set_name(identifier_arg.getTableName());
581
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
 
855
  table_proto->set_name(identifier_arg.getTableName());
 
856
  table_proto->set_schema(identifier_arg.getSchemaName());
582
857
}
583
858
 
584
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
 
859
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
585
860
{
586
 
  drizzled::error_t local_error= EE_OK;
 
861
  int local_error= 0;
587
862
 
588
863
  if (! table.IsInitialized())
589
864
  {
590
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
591
 
             table.name().empty() ? " " :  table.name().c_str(),
592
 
             table.InitializationErrorString().c_str());
593
 
 
 
865
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
866
    return ER_CORRUPT_TABLE_DEFINITION;
595
867
  }
596
868
 
597
 
  setTableMessage(table);
 
869
  setTableProto(new(nothrow) message::Table(table));
598
870
 
599
871
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
872
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
615
887
  db_create_options= (local_db_create_options & 0x0000FFFF);
616
888
  db_options_in_use= db_create_options;
617
889
 
 
890
  row_type= table_options.has_row_type() ?
 
891
    (enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
 
892
 
618
893
  block_size= table_options.has_block_size() ?
619
894
    table_options.block_size() : 0;
620
895
 
621
896
  table_charset= get_charset(table_options.collation_id());
622
897
 
623
 
  if (not table_charset)
 
898
  if (!table_charset)
624
899
  {
625
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
626
 
             table_options.collation().c_str(),
627
 
             table.name().c_str());
 
900
    char errmsg[100];
 
901
    snprintf(errmsg, sizeof(errmsg),
 
902
             _("Table %s has invalid/unknown collation: %d,%s"),
 
903
             getPath(),
 
904
             table_options.collation_id(),
 
905
             table_options.collation().c_str());
 
906
    errmsg[99]='\0';
628
907
 
629
 
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
 
908
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
909
    return ER_CORRUPT_TABLE_DEFINITION;
630
910
  }
631
911
 
632
912
  db_record_offset= 1;
633
913
 
 
914
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
915
 
634
916
  keys= table.indexes_size();
635
917
 
636
918
  key_parts= 0;
736
1018
 
737
1019
      key_part->length= part.compare_length();
738
1020
 
739
 
      int mbmaxlen= 1;
740
 
 
741
 
      if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
742
 
          || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
743
 
      {
744
 
        uint32_t collation_id;
745
 
 
746
 
        if (table.field(part.fieldnr()).string_options().has_collation_id())
747
 
          collation_id= table.field(part.fieldnr()).string_options().collation_id();
748
 
        else
749
 
          collation_id= table.options().collation_id();
750
 
 
751
 
        const CHARSET_INFO *cs= get_charset(collation_id);
752
 
 
753
 
        mbmaxlen= cs->mbmaxlen;
754
 
      }
755
 
      key_part->length*= mbmaxlen;
756
 
 
757
1021
      key_part->store_length= key_part->length;
758
1022
 
759
1023
      /* key_part->offset is set later */
780
1044
  keys_for_keyread.reset();
781
1045
  set_prefix(keys_in_use, keys);
782
1046
 
783
 
  _field_size= table.field_size();
 
1047
  fields= table.field_size();
784
1048
 
785
 
  setFields(_field_size + 1);
786
 
  _fields[_field_size]= NULL;
 
1049
  setFields(fields + 1);
 
1050
  field[fields]= NULL;
787
1051
 
788
1052
  uint32_t local_null_fields= 0;
789
1053
  reclength= 0;
790
1054
 
791
 
  std::vector<uint32_t> field_offsets;
792
 
  std::vector<uint32_t> field_pack_length;
 
1055
  vector<uint32_t> field_offsets;
 
1056
  vector<uint32_t> field_pack_length;
793
1057
 
794
 
  field_offsets.resize(_field_size);
795
 
  field_pack_length.resize(_field_size);
 
1058
  field_offsets.resize(fields);
 
1059
  field_pack_length.resize(fields);
796
1060
 
797
1061
  uint32_t interval_count= 0;
798
1062
  uint32_t interval_parts= 0;
799
1063
 
800
1064
  uint32_t stored_columns_reclength= 0;
801
1065
 
802
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1066
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
803
1067
  {
804
1068
    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
 
    }
 
1069
    if (pfield.constraints().is_nullable())
 
1070
      local_null_fields++;
813
1071
 
814
 
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
 
1072
    enum_field_types drizzle_field_type=
 
1073
      proto_field_type_to_drizzle_type(pfield.type());
815
1074
 
816
1075
    field_offsets[fieldnr]= stored_columns_reclength;
817
1076
 
840
1099
      {
841
1100
        message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
842
1101
 
843
 
        field_pack_length[fieldnr]= 4;
 
1102
        field_pack_length[fieldnr]=
 
1103
          get_enum_pack_length(field_options.field_value_size());
844
1104
 
845
1105
        interval_count++;
846
1106
        interval_parts+= field_options.field_value_size();
850
1110
      {
851
1111
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
852
1112
 
853
 
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
 
1113
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
854
1114
      }
855
1115
      break;
856
1116
    default:
900
1160
 
901
1161
  uint32_t interval_nr= 0;
902
1162
 
903
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1163
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
904
1164
  {
905
1165
    message::Table::Field pfield= table.field(fieldnr);
906
1166
 
912
1172
 
913
1173
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
914
1174
    {
915
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
 
1175
      char errmsg[100];
 
1176
      snprintf(errmsg, sizeof(errmsg),
 
1177
               _("ENUM column %s has greater than %d possible values"),
 
1178
               pfield.name().c_str(),
 
1179
               Field_enum::max_supported_elements);
 
1180
      errmsg[99]='\0';
916
1181
 
917
 
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
 
1182
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
1183
      return ER_CORRUPT_TABLE_DEFINITION;
918
1184
    }
919
1185
 
920
1186
 
957
1223
  /* and read the fields */
958
1224
  interval_nr= 0;
959
1225
 
960
 
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
 
1226
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
961
1227
 
962
1228
  unsigned char* null_pos= getDefaultValues();
963
1229
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
964
1230
 
965
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
1231
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
966
1232
  {
967
1233
    message::Table::Field pfield= table.field(fieldnr);
968
1234
 
975
1241
    }
976
1242
 
977
1243
    if (pfield.has_options() &&
978
 
        pfield.options().has_default_expression() &&
979
 
        pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
 
1244
        pfield.options().has_default_value() &&
 
1245
        pfield.options().default_value().compare("NOW()") == 0)
980
1246
    {
981
 
      if (pfield.options().has_update_expression() &&
982
 
          pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
1247
      if (pfield.options().has_update_value() &&
 
1248
          pfield.options().update_value().compare("NOW()") == 0)
983
1249
      {
984
1250
        unireg_type= Field::TIMESTAMP_DNUN_FIELD;
985
1251
      }
986
 
      else if (! pfield.options().has_update_expression())
 
1252
      else if (! pfield.options().has_update_value())
987
1253
      {
988
1254
        unireg_type= Field::TIMESTAMP_DN_FIELD;
989
1255
      }
990
1256
      else
991
 
      {
992
 
        assert(0); // Invalid update value.
993
 
        abort();
994
 
      }
 
1257
        assert(1); // Invalid update value.
995
1258
    }
996
1259
    else if (pfield.has_options() &&
997
 
             pfield.options().has_update_expression() &&
998
 
             pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
1260
             pfield.options().has_update_value() &&
 
1261
             pfield.options().update_value().compare("NOW()") == 0)
999
1262
    {
1000
1263
      unireg_type= Field::TIMESTAMP_UN_FIELD;
1001
1264
    }
1017
1280
 
1018
1281
    enum_field_types field_type;
1019
1282
 
1020
 
    field_type= proto_field_type_to_drizzle_type(pfield);
 
1283
    field_type= proto_field_type_to_drizzle_type(pfield.type());
1021
1284
 
1022
1285
    const CHARSET_INFO *charset= &my_charset_bin;
1023
1286
 
1062
1325
      {
1063
1326
        if (fo.scale() > DECIMAL_MAX_SCALE)
1064
1327
        {
1065
 
          local_error= ER_NOT_FORM_FILE;
 
1328
          local_error= 4;
1066
1329
 
1067
 
          return true;
 
1330
          return local_error;
1068
1331
        }
1069
1332
        decimals= static_cast<uint8_t>(fo.scale());
1070
1333
      }
1073
1336
    Item *default_value= NULL;
1074
1337
 
1075
1338
    if (pfield.options().has_default_value() ||
1076
 
        pfield.options().default_null()  ||
 
1339
        pfield.options().has_default_null()  ||
1077
1340
        pfield.options().has_default_bin_value())
1078
1341
    {
1079
1342
      default_value= default_value_item(field_type,
1084
1347
    }
1085
1348
 
1086
1349
 
 
1350
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
 
1351
    memset(&temp_table, 0, sizeof(temp_table));
 
1352
    temp_table.setShare(this);
 
1353
    temp_table.in_use= &session;
 
1354
    temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
 
1355
    temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
 
1356
 
1087
1357
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1088
1358
 
1089
 
    // We set field_length in this loop.
1090
1359
    switch (field_type)
1091
1360
    {
1092
1361
    case DRIZZLE_TYPE_BLOB:
1118
1387
            decimals != NOT_FIXED_DEC)
1119
1388
        {
1120
1389
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1121
 
          local_error= ER_M_BIGGER_THAN_D;
1122
 
          return true;
 
1390
          local_error= 1;
 
1391
 
 
1392
          return local_error;
1123
1393
        }
1124
1394
        break;
1125
1395
      }
1127
1397
      {
1128
1398
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1129
1399
 
1130
 
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1400
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1131
1401
                                                     false);
1132
1402
        break;
1133
1403
      }
 
1404
    case DRIZZLE_TYPE_TIMESTAMP:
1134
1405
    case DRIZZLE_TYPE_DATETIME:
1135
1406
      field_length= DateTime::MAX_STRING_LENGTH;
1136
1407
      break;
1163
1434
      }
1164
1435
      break;
1165
1436
    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();
 
1437
      field_length= MAX_BIGINT_WIDTH;
1185
1438
      break;
1186
1439
    case DRIZZLE_TYPE_NULL:
1187
1440
      abort(); // Programming error
1188
1441
    }
1189
1442
 
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
 
    }
1236
 
 
1237
 
    // This needs to go, we should be setting the "use" on the field so that
1238
 
    // it does not reference the share/table.
1239
 
    table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1240
 
    temp_table.in_use= &session;
 
1443
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
1444
                                field_length,
 
1445
                                pfield.constraints().is_nullable(),
 
1446
                                null_pos,
 
1447
                                null_bit_pos,
 
1448
                                decimals,
 
1449
                                field_type,
 
1450
                                charset,
 
1451
                                (Field::utype) MTYP_TYPENR(unireg_type),
 
1452
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1453
                                 &intervals[interval_nr++]
 
1454
                                 : (TYPELIB*) 0),
 
1455
                                getTableProto()->field(fieldnr).name().c_str());
 
1456
 
 
1457
    field[fieldnr]= f;
1241
1458
 
1242
1459
    f->init(&temp_table); /* blob default values need table obj */
1243
1460
 
1258
1475
      if (res != 0 && res != 3) /* @TODO Huh? */
1259
1476
      {
1260
1477
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1261
 
        local_error= ER_INVALID_DEFAULT;
 
1478
        local_error= 1;
1262
1479
 
1263
 
        return true;
 
1480
        return local_error;
1264
1481
      }
1265
1482
    }
1266
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1483
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1484
             (f->flags & NOT_NULL_FLAG))
1267
1485
    {
1268
1486
      f->set_notnull();
1269
1487
      f->store((int64_t) 1, true);
1277
1495
    f->setTable(NULL);
1278
1496
    f->orig_table= NULL;
1279
1497
 
1280
 
    f->setPosition(fieldnr);
 
1498
    f->field_index= fieldnr;
1281
1499
    f->comment= comment;
1282
 
    if (not default_value &&
1283
 
        not (f->unireg_check==Field::NEXT_NUMBER) &&
 
1500
    if (! default_value &&
 
1501
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1284
1502
        (f->flags & NOT_NULL_FLAG) &&
1285
 
        (not f->is_timestamp()))
 
1503
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1286
1504
    {
1287
1505
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1288
1506
    }
1289
1507
 
1290
1508
    if (f->unireg_check == Field::NEXT_NUMBER)
1291
 
      found_next_number_field= &(_fields[fieldnr]);
 
1509
      found_next_number_field= &(field[fieldnr]);
 
1510
 
 
1511
    if (timestamp_field == f)
 
1512
      timestamp_field_offset= fieldnr;
1292
1513
 
1293
1514
    if (use_hash) /* supposedly this never fails... but comments lie */
1294
1515
    {
1295
 
      const char *local_field_name= _fields[fieldnr]->field_name;
1296
 
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
 
1516
      const char *local_field_name= field[fieldnr]->field_name;
 
1517
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1297
1518
    }
 
1519
 
1298
1520
  }
1299
1521
 
1300
1522
  keyinfo= key_info;
1319
1541
    We need to set the unused bits to 1. If the number of bits is a multiple
1320
1542
    of 8 there are no unused bits.
1321
1543
  */
 
1544
 
1322
1545
  if (null_count & 7)
1323
1546
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1324
1547
 
1349
1572
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1350
1573
        {
1351
1574
          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)
 
1575
          if (! fieldnr ||
 
1576
              field[fieldnr-1]->null_ptr ||
 
1577
              field[fieldnr-1]->key_length() != key_part[i].length)
1355
1578
          {
1356
1579
            local_primary_key= MAX_KEY; // Can't be used
1357
1580
            break;
1366
1589
        {
1367
1590
          return ENOMEM;
1368
1591
        }
1369
 
        local_field= key_part->field= _fields[key_part->fieldnr-1];
 
1592
        local_field= key_part->field= field[key_part->fieldnr-1];
1370
1593
        key_part->type= local_field->key_type();
1371
1594
        if (local_field->null_ptr)
1372
1595
        {
1472
1695
                            &next_number_keypart)) < 0)
1473
1696
    {
1474
1697
      /* Wrong field definition */
1475
 
      local_error= ER_NOT_FORM_FILE;
 
1698
      local_error= 4;
1476
1699
 
1477
 
      return true;
 
1700
      return local_error;
1478
1701
    }
1479
1702
    else
1480
1703
    {
1484
1707
 
1485
1708
  if (blob_fields)
1486
1709
  {
 
1710
    uint32_t k, *save;
 
1711
 
1487
1712
    /* Store offsets to blob fields to find them fast */
1488
1713
    blob_field.resize(blob_fields);
1489
 
    uint32_t *save= &blob_field[0];
1490
 
    uint32_t k= 0;
1491
 
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
 
1714
    save= &blob_field[0];
 
1715
    k= 0;
 
1716
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1492
1717
    {
1493
1718
      if ((*iter)->flags & BLOB_FLAG)
1494
1719
        (*save++)= k;
1495
1720
    }
1496
1721
  }
1497
1722
 
1498
 
  all_set.clear();
1499
 
  all_set.resize(_field_size);
1500
 
  all_set.set();
1501
 
 
1502
 
  return local_error != EE_OK;
1503
 
}
 
1723
  db_low_byte_first= true; // @todo Question this.
 
1724
  column_bitmap_size= bitmap_buffer_size(fields);
 
1725
 
 
1726
  all_bitmap.resize(column_bitmap_size);
 
1727
  all_set.init(&all_bitmap[0], fields);
 
1728
  all_set.setAll();
 
1729
 
 
1730
  return local_error;
 
1731
}
 
1732
 
 
1733
int TableShare::parse_table_proto(Session& session, message::Table &table)
 
1734
{
 
1735
  int local_error= inner_parse_table_proto(session, table);
 
1736
 
 
1737
  if (not local_error)
 
1738
    return 0;
 
1739
 
 
1740
  error= local_error;
 
1741
  open_errno= errno;
 
1742
  errarg= 0;
 
1743
  open_table_error(local_error, open_errno, 0);
 
1744
 
 
1745
  return local_error;
 
1746
}
 
1747
 
1504
1748
 
1505
1749
/*
1506
1750
  Read table definition from a binary / text based .frm cursor
1512
1756
 
1513
1757
  NOTES
1514
1758
  This function is called when the table definition is not cached in
1515
 
  definition::Cache::singleton().getCache()
 
1759
  table_def_cache
1516
1760
  The data is returned in 'share', which is alloced by
1517
1761
  alloc_table_share().. The code assumes that share is initialized.
1518
1762
 
1526
1770
  6    Unknown .frm version
1527
1771
*/
1528
1772
 
1529
 
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
 
1773
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1530
1774
{
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())
 
1775
  int local_error;
 
1776
  bool error_given;
 
1777
 
 
1778
  local_error= 1;
 
1779
  error_given= 0;
 
1780
 
 
1781
  message::Table table;
 
1782
 
 
1783
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
 
1784
 
 
1785
  if (local_error != EEXIST)
1536
1786
  {
1537
 
    if (parse_table_proto(session, *table))
 
1787
    if (local_error > 0)
1538
1788
    {
1539
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1789
      errno= local_error;
 
1790
      local_error= 1;
1541
1791
    }
1542
1792
    else
1543
1793
    {
1544
 
      setTableCategory(TABLE_CATEGORY_USER);
1545
 
      local_error= EE_OK;
 
1794
      if (not table.IsInitialized())
 
1795
      {
 
1796
        local_error= 4;
 
1797
      }
1546
1798
    }
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);
 
1799
    goto err_not_open;
 
1800
  }
 
1801
 
 
1802
  local_error= parse_table_proto(session, table);
 
1803
 
 
1804
  setTableCategory(TABLE_CATEGORY_USER);
 
1805
 
 
1806
err_not_open:
 
1807
  if (local_error && !error_given)
 
1808
  {
 
1809
    error= local_error;
 
1810
    open_table_error(error, (open_errno= errno), 0);
 
1811
  }
 
1812
 
 
1813
  return(error);
1560
1814
}
1561
1815
 
1562
1816
 
1583
1837
  5    Error (see open_table_error: charset unavailable)
1584
1838
  7    Table definition has changed in engine
1585
1839
*/
 
1840
 
1586
1841
int TableShare::open_table_from_share(Session *session,
1587
 
                                      const identifier::Table &identifier,
 
1842
                                      const TableIdentifier &identifier,
1588
1843
                                      const char *alias,
1589
1844
                                      uint32_t db_stat, uint32_t ha_open_flags,
1590
1845
                                      Table &outparam)
1591
1846
{
 
1847
  int local_error;
 
1848
  uint32_t records, bitmap_size;
1592
1849
  bool error_reported= false;
1593
 
  int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1594
 
 
1595
 
  if (not ret)
1596
 
    ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1597
 
 
1598
 
  if (not ret)
1599
 
    return ret;
1600
 
 
1601
 
  if (not error_reported)
1602
 
    open_table_error(ret, errno, 0);
1603
 
 
1604
 
  boost::checked_delete(outparam.cursor);
1605
 
  outparam.cursor= 0;                           // For easier error checking
1606
 
  outparam.db_stat= 0;
1607
 
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1608
 
  outparam.clearAlias();
1609
 
 
1610
 
  return ret;
1611
 
}
1612
 
 
1613
 
int TableShare::open_table_from_share_inner(Session *session,
1614
 
                                            const char *alias,
1615
 
                                            uint32_t db_stat,
1616
 
                                            Table &outparam)
1617
 
{
1618
 
  int local_error;
1619
 
  uint32_t records;
1620
 
  unsigned char *record= NULL;
 
1850
  unsigned char *record, *bitmaps;
1621
1851
  Field **field_ptr;
1622
1852
 
 
1853
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1854
  assert(session->lex->is_lex_started);
 
1855
 
1623
1856
  local_error= 1;
1624
1857
  outparam.resetTable(session, this, db_stat);
1625
1858
 
1626
 
  outparam.setAlias(alias);
 
1859
 
 
1860
  if (not (outparam.alias= strdup(alias)))
 
1861
    goto err;
1627
1862
 
1628
1863
  /* Allocate Cursor */
1629
 
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
1630
 
    return local_error;
 
1864
  if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
 
1865
    goto err;
1631
1866
 
1632
1867
  local_error= 4;
1633
1868
  records= 0;
1637
1872
  records++;
1638
1873
 
1639
1874
  if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1640
 
    return local_error;
 
1875
    goto err;
1641
1876
 
1642
1877
  if (records == 0)
1643
1878
  {
1650
1885
    if (records > 1)
1651
1886
      outparam.record[1]= record+ rec_buff_length;
1652
1887
    else
1653
 
      outparam.record[1]= outparam.getInsertRecord();   // Safety
 
1888
      outparam.record[1]= outparam.record[0];   // Safety
1654
1889
  }
1655
1890
 
1656
 
#ifdef HAVE_VALGRIND
 
1891
#ifdef HAVE_purify
1657
1892
  /*
1658
1893
    We need this because when we read var-length rows, we are not updating
1659
1894
    bytes after end of varchar
1660
1895
  */
1661
1896
  if (records > 1)
1662
1897
  {
1663
 
    memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1664
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1898
    memcpy(outparam.record[0], getDefaultValues(), rec_buff_length);
 
1899
    memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1665
1900
    if (records > 2)
1666
 
      memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
 
1901
      memcpy(outparam.record[1], getDefaultValues(), rec_buff_length);
1667
1902
  }
1668
1903
#endif
1669
1904
  if (records > 1)
1670
1905
  {
1671
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1906
    memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1672
1907
  }
1673
1908
 
1674
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
 
1909
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1675
1910
  {
1676
 
    return local_error;
 
1911
    goto err;
1677
1912
  }
1678
1913
 
1679
1914
  outparam.setFields(field_ptr);
1680
1915
 
1681
 
  record= (unsigned char*) outparam.getInsertRecord()-1;        /* Fieldstart = 1 */
 
1916
  record= (unsigned char*) outparam.record[0]-1;        /* Fieldstart = 1 */
1682
1917
 
1683
1918
  outparam.null_flags= (unsigned char*) record+1;
1684
1919
 
1685
1920
  /* 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++)
 
1921
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1687
1922
  {
1688
 
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1689
 
      return local_error;
 
1923
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1924
      goto err;
1690
1925
  }
1691
1926
  (*field_ptr)= 0;                              // End marker
1692
1927
 
1694
1929
    outparam.found_next_number_field=
1695
1930
      outparam.getField(positionFields(found_next_number_field));
1696
1931
  if (timestamp_field)
1697
 
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
 
1932
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1933
 
1698
1934
 
1699
1935
  /* Fix key->name and key_part->field */
1700
1936
  if (key_parts)
1704
1940
    uint32_t n_length;
1705
1941
    n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1706
1942
    if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1707
 
      return local_error;
 
1943
      goto err;
1708
1944
    outparam.key_info= local_key_info;
1709
1945
    key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1710
1946
 
1743
1979
 
1744
1980
  /* Allocate bitmaps */
1745
1981
 
1746
 
  outparam.def_read_set.resize(_field_size);
1747
 
  outparam.def_write_set.resize(_field_size);
1748
 
  outparam.tmp_set.resize(_field_size);
 
1982
  bitmap_size= column_bitmap_size;
 
1983
  if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
 
1984
  {
 
1985
    goto err;
 
1986
  }
 
1987
  outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
 
1988
  outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
 
1989
  outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1749
1990
  outparam.default_column_bitmaps();
1750
1991
 
1751
 
  return 0;
1752
 
}
1753
 
 
1754
 
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1755
 
                                        uint32_t db_stat, uint32_t ha_open_flags,
1756
 
                                        Table &outparam,
1757
 
                                        bool &error_reported)
1758
 
{
1759
1992
  /* The table struct is now initialized;  Open the table */
1760
 
  int local_error= 2;
 
1993
  local_error= 2;
1761
1994
  if (db_stat)
1762
1995
  {
1763
1996
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
1764
1997
    int ha_err;
1765
1998
 
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))))
 
1999
    if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam, getNormalizedPath(),
 
2000
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
2001
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1769
2002
    {
1770
2003
      switch (ha_err)
1771
2004
      {
1792
2025
          local_error= 7;
1793
2026
        break;
1794
2027
      }
1795
 
      return local_error;
 
2028
      goto err;
1796
2029
    }
1797
2030
  }
1798
2031
 
 
2032
#if defined(HAVE_purify)
 
2033
  memset(bitmaps, 0, bitmap_size*3);
 
2034
#endif
 
2035
 
1799
2036
  return 0;
 
2037
 
 
2038
err:
 
2039
  if (!error_reported)
 
2040
    open_table_error(local_error, errno, 0);
 
2041
 
 
2042
  delete outparam.cursor;
 
2043
  outparam.cursor= 0;                           // For easier error checking
 
2044
  outparam.db_stat= 0;
 
2045
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
 
2046
  free((char*) outparam.alias);
 
2047
  return (local_error);
1800
2048
}
1801
2049
 
1802
2050
/* error message when opening a form cursor */
1803
2051
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1804
2052
{
 
2053
  int err_no;
1805
2054
  char buff[FN_REFLEN];
1806
2055
  myf errortype= ME_ERROR+ME_WAITTANG;
1807
2056
 
1810
2059
  case 1:
1811
2060
    if (db_errno == ENOENT)
1812
2061
    {
1813
 
      identifier::Table identifier(db.str, table_name.str);
1814
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
2062
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1815
2063
    }
1816
2064
    else
1817
2065
    {
1822
2070
    break;
1823
2071
  case 2:
1824
2072
    {
1825
 
      drizzled::error_t err_no;
1826
 
 
1827
2073
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1828
2074
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1829
 
 
1830
2075
      my_error(err_no, errortype, normalized_path.str, db_errno);
1831
2076
      break;
1832
2077
    }
1862
2107
  return;
1863
2108
} /* open_table_error */
1864
2109
 
1865
 
Field *TableShare::make_field(const message::Table::Field &pfield,
1866
 
                              unsigned char *ptr,
 
2110
Field *TableShare::make_field(unsigned char *ptr,
1867
2111
                              uint32_t field_length,
1868
2112
                              bool is_nullable,
1869
2113
                              unsigned char *null_pos,
1875
2119
                              TYPELIB *interval,
1876
2120
                              const char *field_name)
1877
2121
{
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
2122
  if (! is_nullable)
1908
2123
  {
1909
2124
    null_pos=0;
1914
2129
    null_bit= ((unsigned char) 1) << null_bit;
1915
2130
  }
1916
2131
 
 
2132
  switch (field_type) 
 
2133
  {
 
2134
  case DRIZZLE_TYPE_DATE:
 
2135
  case DRIZZLE_TYPE_DATETIME:
 
2136
  case DRIZZLE_TYPE_TIMESTAMP:
 
2137
    field_charset= &my_charset_bin;
 
2138
  default: break;
 
2139
  }
 
2140
 
1917
2141
  switch (field_type)
1918
2142
  {
1919
2143
  case DRIZZLE_TYPE_ENUM:
1920
2144
    return new (&mem_root) Field_enum(ptr,
1921
 
                                      field_length,
1922
 
                                      null_pos,
1923
 
                                      null_bit,
1924
 
                                      field_name,
1925
 
                                      interval,
1926
 
                                      field_charset);
 
2145
                                 field_length,
 
2146
                                 null_pos,
 
2147
                                 null_bit,
 
2148
                                 field_name,
 
2149
                                 get_enum_pack_length(interval->count),
 
2150
                                 interval,
 
2151
                                 field_charset);
1927
2152
  case DRIZZLE_TYPE_VARCHAR:
1928
 
    setVariableWidth();
1929
2153
    return new (&mem_root) Field_varstring(ptr,field_length,
1930
 
                                      ha_varchar_packlength(field_length),
 
2154
                                      HA_VARCHAR_PACKLENGTH(field_length),
1931
2155
                                      null_pos,null_bit,
1932
2156
                                      field_name,
 
2157
                                      this,
1933
2158
                                      field_charset);
1934
2159
  case DRIZZLE_TYPE_BLOB:
1935
2160
    return new (&mem_root) Field_blob(ptr,
1936
 
                                      null_pos,
1937
 
                                      null_bit,
1938
 
                                      field_name,
1939
 
                                      this,
1940
 
                                      field_charset);
 
2161
                                 null_pos,
 
2162
                                 null_bit,
 
2163
                                 field_name,
 
2164
                                 this,
 
2165
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
2166
                                 field_charset);
1941
2167
  case DRIZZLE_TYPE_DECIMAL:
1942
2168
    return new (&mem_root) Field_decimal(ptr,
1943
 
                                         field_length,
1944
 
                                         null_pos,
1945
 
                                         null_bit,
1946
 
                                         unireg_check,
1947
 
                                         field_name,
1948
 
                                         decimals);
 
2169
                                    field_length,
 
2170
                                    null_pos,
 
2171
                                    null_bit,
 
2172
                                    unireg_check,
 
2173
                                    field_name,
 
2174
                                    decimals,
 
2175
                                    false,
 
2176
                                    false /* is_unsigned */);
1949
2177
  case DRIZZLE_TYPE_DOUBLE:
1950
2178
    return new (&mem_root) Field_double(ptr,
1951
2179
                                   field_length,
1956
2184
                                   decimals,
1957
2185
                                   false,
1958
2186
                                   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
2187
  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);
 
2188
    return new (&mem_root) Field_long(ptr,
 
2189
                                 field_length,
 
2190
                                 null_pos,
 
2191
                                 null_bit,
 
2192
                                 unireg_check,
 
2193
                                 field_name,
 
2194
                                 false,
 
2195
                                 false /* is_unsigned */);
1979
2196
  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);
 
2197
    return new (&mem_root) Field_int64_t(ptr,
 
2198
                                    field_length,
 
2199
                                    null_pos,
 
2200
                                    null_bit,
 
2201
                                    unireg_check,
 
2202
                                    field_name,
 
2203
                                    false,
 
2204
                                    false /* is_unsigned */);
2005
2205
  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);
 
2206
    return new (&mem_root) Field_timestamp(ptr,
 
2207
                                      field_length,
 
2208
                                      null_pos,
 
2209
                                      null_bit,
 
2210
                                      unireg_check,
 
2211
                                      field_name,
 
2212
                                      this,
 
2213
                                      field_charset);
2018
2214
  case DRIZZLE_TYPE_DATE:
2019
2215
    return new (&mem_root) Field_date(ptr,
2020
2216
                                 null_pos,
2021
2217
                                 null_bit,
2022
 
                                 field_name);
 
2218
                                 field_name,
 
2219
                                 field_charset);
2023
2220
  case DRIZZLE_TYPE_DATETIME:
2024
2221
    return new (&mem_root) Field_datetime(ptr,
2025
2222
                                     null_pos,
2026
2223
                                     null_bit,
2027
 
                                     field_name);
 
2224
                                     field_name,
 
2225
                                     field_charset);
2028
2226
  case DRIZZLE_TYPE_NULL:
2029
2227
    return new (&mem_root) Field_null(ptr,
2030
 
                                      field_length,
2031
 
                                      field_name);
 
2228
                                 field_length,
 
2229
                                 field_name,
 
2230
                                 field_charset);
 
2231
  default: // Impossible (Wrong version)
 
2232
    break;
2032
2233
  }
2033
 
  assert(0);
2034
 
  abort();
2035
 
}
2036
 
 
2037
 
void TableShare::refreshVersion()
2038
 
{
2039
 
  version= refresh_version;
 
2234
  return 0;
2040
2235
}
2041
2236
 
2042
2237