~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table_share.cc

  • Committer: Brian Aker
  • Date: 2010-06-05 00:15:57 UTC
  • mfrom: (1589.1.1 drizzle_events)
  • Revision ID: brian@gaz-20100605001557-j1k41ni5k9mis891
Merge in Barry.

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
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
    const string key_string(share->getCacheKey(),
 
150
                            share->getCacheKeySize());
 
151
    plugin::EventObserver::deregisterTableEvents(*share);
 
152
   
 
153
    TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
154
    if (iter != table_def_cache.end())
 
155
    {
 
156
      table_def_cache.erase(iter);
 
157
      delete share;
 
158
    }
 
159
    return;
 
160
  }
 
161
  pthread_mutex_unlock(&share->mutex);
 
162
}
 
163
 
 
164
void TableShare::release(const char *key, uint32_t key_length)
 
165
{
 
166
  const string key_string(key, key_length);
 
167
 
 
168
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
169
  if (iter != table_def_cache.end())
 
170
  {
 
171
    TableShare *share= (*iter).second;
 
172
    share->version= 0;                          // Mark for delete
 
173
    if (share->ref_count == 0)
 
174
    {
 
175
      pthread_mutex_lock(&share->mutex);
 
176
      plugin::EventObserver::deregisterTableEvents(*share);
 
177
      table_def_cache.erase(key_string);
 
178
      delete share;
 
179
    }
 
180
  }
 
181
}
 
182
 
 
183
 
 
184
static TableShare *foundTableShare(TableShare *share)
 
185
{
 
186
  /*
 
187
    We found an existing table definition. Return it if we didn't get
 
188
    an error when reading the table definition from file.
 
189
  */
 
190
 
 
191
  /* We must do a lock to ensure that the structure is initialized */
 
192
  (void) pthread_mutex_lock(&share->mutex);
 
193
  if (share->error)
 
194
  {
 
195
    /* Table definition contained an error */
 
196
    share->open_table_error(share->error, share->open_errno, share->errarg);
 
197
    (void) pthread_mutex_unlock(&share->mutex);
 
198
 
 
199
    return NULL;
 
200
  }
 
201
 
 
202
  share->incrementTableCount();
 
203
  (void) pthread_mutex_unlock(&share->mutex);
 
204
 
 
205
  return share;
 
206
}
 
207
 
 
208
/*
 
209
  Get TableShare for a table.
 
210
 
 
211
  get_table_share()
 
212
  session                       Thread handle
 
213
  table_list            Table that should be opened
 
214
  key                   Table cache key
 
215
  key_length            Length of key
 
216
  error                 out: Error code from open_table_def()
 
217
 
 
218
  IMPLEMENTATION
 
219
  Get a table definition from the table definition cache.
 
220
  If it doesn't exist, create a new from the table definition file.
 
221
 
 
222
  NOTES
 
223
  We must have wrlock on LOCK_open when we come here
 
224
  (To be changed later)
 
225
 
 
226
  RETURN
 
227
  0  Error
 
228
#  Share for table
 
229
*/
 
230
 
 
231
TableShare *TableShare::getShare(Session *session, 
 
232
                                 char *key,
 
233
                                 uint32_t key_length, int *error)
 
234
{
 
235
  const string key_string(key, key_length);
 
236
  TableShare *share= NULL;
 
237
 
 
238
  *error= 0;
 
239
 
 
240
  /* Read table definition from cache */
 
241
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
242
  if (iter != table_def_cache.end())
 
243
  {
 
244
    share= (*iter).second;
 
245
    return foundTableShare(share);
 
246
  }
 
247
 
 
248
  if (not (share= new TableShare(key, key_length)))
 
249
  {
 
250
    return NULL;
 
251
  }
 
252
 
 
253
  /*
 
254
    Lock mutex to be able to read table definition from file without
 
255
    conflicts
 
256
  */
 
257
  (void) pthread_mutex_lock(&share->mutex);
 
258
 
 
259
  /**
 
260
   * @TODO: we need to eject something if we exceed table_def_size
 
261
 */
 
262
  pair<TableDefinitionCache::iterator, bool> ret=
 
263
    table_def_cache.insert(make_pair(key_string, share));
 
264
  if (ret.second == false)
 
265
  {
 
266
    delete share;
 
267
 
 
268
    return NULL;
 
269
  }
 
270
 
 
271
  TableIdentifier identifier(share->getSchemaName(), share->getTableName());
 
272
  if (share->open_table_def(*session, identifier))
 
273
  {
 
274
    *error= share->error;
 
275
    table_def_cache.erase(key_string);
 
276
    delete share;
 
277
 
 
278
    return NULL;
 
279
  }
 
280
  share->ref_count++;                           // Mark in use
 
281
  
 
282
  plugin::EventObserver::registerTableEvents(*share);
 
283
  
 
284
  (void) pthread_mutex_unlock(&share->mutex);
 
285
 
 
286
  return share;
 
287
}
 
288
 
 
289
 
 
290
/*
 
291
  Check if table definition exits in cache
 
292
 
 
293
  SYNOPSIS
 
294
  get_cached_table_share()
 
295
  db                    Database name
 
296
  table_name            Table name
 
297
 
 
298
  RETURN
 
299
  0  Not cached
 
300
#  TableShare for table
 
301
*/
 
302
TableShare *TableShare::getShare(TableIdentifier &identifier)
 
303
{
 
304
  char key[MAX_DBKEY_LENGTH];
 
305
  uint32_t key_length;
 
306
  safe_mutex_assert_owner(&LOCK_open);
 
307
 
 
308
  key_length= TableShare::createKey(key, identifier);
 
309
 
 
310
  const string key_string(key, key_length);
 
311
 
 
312
  TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
 
313
  if (iter != table_def_cache.end())
 
314
  {
 
315
    return (*iter).second;
 
316
  }
 
317
  else
 
318
  {
 
319
    return NULL;
 
320
  }
 
321
}
 
322
 
 
323
/* Get column name from column hash */
 
324
 
 
325
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
 
326
{
 
327
  *length= (uint32_t) strlen((*buff)->field_name);
 
328
  return (unsigned char*) (*buff)->field_name;
 
329
}
 
330
 
 
331
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
 
332
{
 
333
  enum_field_types field_type;
 
334
 
 
335
  switch(proto_field_type)
102
336
  {
103
337
  case message::Table::Field::INTEGER:
104
 
    return DRIZZLE_TYPE_LONG;
105
 
 
 
338
    field_type= DRIZZLE_TYPE_LONG;
 
339
    break;
106
340
  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
 
 
 
341
    field_type= DRIZZLE_TYPE_DOUBLE;
 
342
    break;
 
343
  case message::Table::Field::TIMESTAMP:
 
344
    field_type= DRIZZLE_TYPE_TIMESTAMP;
 
345
    break;
115
346
  case message::Table::Field::BIGINT:
116
 
    return DRIZZLE_TYPE_LONGLONG;
117
 
 
 
347
    field_type= DRIZZLE_TYPE_LONGLONG;
 
348
    break;
118
349
  case message::Table::Field::DATETIME:
119
 
    return DRIZZLE_TYPE_DATETIME;
120
 
 
 
350
    field_type= DRIZZLE_TYPE_DATETIME;
 
351
    break;
121
352
  case message::Table::Field::DATE:
122
 
    return DRIZZLE_TYPE_DATE;
123
 
 
 
353
    field_type= DRIZZLE_TYPE_DATE;
 
354
    break;
124
355
  case message::Table::Field::VARCHAR:
125
 
    return DRIZZLE_TYPE_VARCHAR;
126
 
 
 
356
    field_type= DRIZZLE_TYPE_VARCHAR;
 
357
    break;
127
358
  case message::Table::Field::DECIMAL:
128
 
    return DRIZZLE_TYPE_DECIMAL;
129
 
 
 
359
    field_type= DRIZZLE_TYPE_DECIMAL;
 
360
    break;
130
361
  case message::Table::Field::ENUM:
131
 
    return DRIZZLE_TYPE_ENUM;
132
 
 
 
362
    field_type= DRIZZLE_TYPE_ENUM;
 
363
    break;
133
364
  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;
 
365
    field_type= DRIZZLE_TYPE_BLOB;
 
366
    break;
 
367
  default:
 
368
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
 
369
    assert(1);
144
370
  }
145
371
 
146
 
  abort();
 
372
  return field_type;
147
373
}
148
374
 
149
375
static Item *default_value_item(enum_field_types field_type,
174
400
                                 default_value->length());
175
401
    break;
176
402
  case DRIZZLE_TYPE_NULL:
177
 
    assert(0);
178
 
    abort();
 
403
    assert(false);
179
404
  case DRIZZLE_TYPE_TIMESTAMP:
180
405
  case DRIZZLE_TYPE_DATETIME:
181
 
  case DRIZZLE_TYPE_TIME:
182
406
  case DRIZZLE_TYPE_DATE:
 
407
    if (default_value->compare("NOW()") == 0)
 
408
      break;
183
409
  case DRIZZLE_TYPE_ENUM:
184
 
  case DRIZZLE_TYPE_UUID:
185
 
  case DRIZZLE_TYPE_MICROTIME:
186
 
  case DRIZZLE_TYPE_BOOLEAN:
187
410
    default_item= new Item_string(default_value->c_str(),
188
411
                                  default_value->length(),
189
412
                                  system_charset_info);
222
445
 */
223
446
bool TableShare::fieldInPrimaryKey(Field *in_field) const
224
447
{
225
 
  assert(getTableMessage());
 
448
  assert(table_proto != NULL);
226
449
 
227
 
  size_t num_indexes= getTableMessage()->indexes_size();
 
450
  size_t num_indexes= table_proto->indexes_size();
228
451
 
229
452
  for (size_t x= 0; x < num_indexes; ++x)
230
453
  {
231
 
    const message::Table::Index &index= getTableMessage()->indexes(x);
 
454
    const message::Table::Index &index= table_proto->indexes(x);
232
455
    if (index.is_primary())
233
456
    {
234
457
      size_t num_parts= index.index_part_size();
235
458
      for (size_t y= 0; y < num_parts; ++y)
236
459
      {
237
 
        if (index.index_part(y).fieldnr() == in_field->position())
 
460
        if (index.index_part(y).fieldnr() == in_field->field_index)
238
461
          return true;
239
462
      }
240
463
    }
242
465
  return false;
243
466
}
244
467
 
245
 
TableShare::TableShare(const identifier::Table::Type type_arg) :
246
 
  table_category(TABLE_UNKNOWN_CATEGORY),
247
 
  found_next_number_field(NULL),
248
 
  timestamp_field(NULL),
249
 
  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),
256
 
  block_size(0),
257
 
  version(0),
258
 
  timestamp_offset(0),
259
 
  reclength(0),
260
 
  stored_rec_length(0),
261
 
  max_rows(0),
262
 
  _table_message(NULL),
263
 
  storage_engine(NULL),
264
 
  tmp_table(type_arg),
265
 
  _ref_count(0),
266
 
  null_bytes(0),
267
 
  last_null_bit_pos(0),
268
 
  _field_size(0),
269
 
  rec_buff_length(0),
270
 
  keys(0),
271
 
  key_parts(0),
272
 
  max_key_length(0),
273
 
  max_unique_length(0),
274
 
  total_key_length(0),
275
 
  uniques(0),
276
 
  null_fields(0),
277
 
  blob_fields(0),
278
 
  has_variable_width(false),
279
 
  db_create_options(0),
280
 
  db_options_in_use(0),
281
 
  db_record_offset(0),
282
 
  rowid_field_offset(0),
283
 
  primary_key(MAX_KEY),
284
 
  next_number_index(0),
285
 
  next_number_key_offset(0),
286
 
  next_number_keypart(0),
287
 
  error(0),
288
 
  open_errno(0),
289
 
  errarg(0),
290
 
  blob_ptr_size(portable_sizeof_char_ptr),
291
 
  db_low_byte_first(false),
292
 
  keys_in_use(0),
293
 
  keys_for_keyread(0)
294
 
{
295
 
  if (type_arg == message::Table::INTERNAL)
296
 
  {
297
 
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
298
 
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
299
 
  }
300
 
  else
301
 
  {
302
 
    init("", "");
303
 
  }
304
 
}
305
 
 
306
 
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
307
 
  table_category(TABLE_UNKNOWN_CATEGORY),
308
 
  found_next_number_field(NULL),
309
 
  timestamp_field(NULL),
310
 
  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),
318
 
  block_size(0),
319
 
  version(0),
320
 
  timestamp_offset(0),
321
 
  reclength(0),
322
 
  stored_rec_length(0),
323
 
  max_rows(0),
324
 
  _table_message(NULL),
325
 
  storage_engine(NULL),
326
 
  tmp_table(message::Table::INTERNAL),
327
 
  _ref_count(0),
328
 
  null_bytes(0),
329
 
  last_null_bit_pos(0),
330
 
  _field_size(0),
331
 
  rec_buff_length(0),
332
 
  keys(0),
333
 
  key_parts(0),
334
 
  max_key_length(0),
335
 
  max_unique_length(0),
336
 
  total_key_length(0),
337
 
  uniques(0),
338
 
  null_fields(0),
339
 
  blob_fields(0),
340
 
  has_variable_width(false),
341
 
  db_create_options(0),
342
 
  db_options_in_use(0),
343
 
  db_record_offset(0),
344
 
  rowid_field_offset(0),
345
 
  primary_key(MAX_KEY),
346
 
  next_number_index(0),
347
 
  next_number_key_offset(0),
348
 
  next_number_keypart(0),
349
 
  error(0),
350
 
  open_errno(0),
351
 
  errarg(0),
352
 
  blob_ptr_size(portable_sizeof_char_ptr),
353
 
  db_low_byte_first(false),
354
 
  keys_in_use(0),
355
 
  keys_for_keyread(0)
356
 
{
357
 
  assert(identifier.getKey() == key);
358
 
 
359
 
  private_key_for_cache= key;
360
 
 
361
 
  table_category=         TABLE_CATEGORY_TEMPORARY;
362
 
  tmp_table=              message::Table::INTERNAL;
363
 
 
364
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
365
 
  db.length= strlen(private_key_for_cache.vector());
366
 
 
367
 
  table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
368
 
  table_name.length= strlen(table_name.str);
369
 
  path.str= (char *)"";
370
 
  normalized_path.str= path.str;
371
 
  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
 
 
377
 
  assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
378
 
}
379
 
 
380
 
 
381
 
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
382
 
  table_category(TABLE_UNKNOWN_CATEGORY),
383
 
  found_next_number_field(NULL),
384
 
  timestamp_field(NULL),
385
 
  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),
393
 
  block_size(0),
394
 
  version(0),
395
 
  timestamp_offset(0),
396
 
  reclength(0),
397
 
  stored_rec_length(0),
398
 
  max_rows(0),
399
 
  _table_message(NULL),
400
 
  storage_engine(NULL),
401
 
  tmp_table(identifier.getType()),
402
 
  _ref_count(0),
403
 
  null_bytes(0),
404
 
  last_null_bit_pos(0),
405
 
  _field_size(0),
406
 
  rec_buff_length(0),
407
 
  keys(0),
408
 
  key_parts(0),
409
 
  max_key_length(0),
410
 
  max_unique_length(0),
411
 
  total_key_length(0),
412
 
  uniques(0),
413
 
  null_fields(0),
414
 
  blob_fields(0),
415
 
  has_variable_width(false),
416
 
  db_create_options(0),
417
 
  db_options_in_use(0),
418
 
  db_record_offset(0),
419
 
  rowid_field_offset(0),
420
 
  primary_key(MAX_KEY),
421
 
  next_number_index(0),
422
 
  next_number_key_offset(0),
423
 
  next_number_keypart(0),
424
 
  error(0),
425
 
  open_errno(0),
426
 
  errarg(0),
427
 
  blob_ptr_size(portable_sizeof_char_ptr),
428
 
  db_low_byte_first(false),
429
 
  keys_in_use(0),
430
 
  keys_for_keyread(0)
431
 
{
432
 
  private_key_for_cache= identifier.getKey();
433
 
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
434
 
  private_normalized_path.resize(identifier.getPath().size() + 1);
435
 
  memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
436
 
 
437
 
  {
438
 
    table_category=         TABLE_CATEGORY_TEMPORARY;
439
 
    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());
442
 
    table_name.str= db.str + 1;
443
 
    table_name.length= strlen(table_name.str);
444
 
    path.str= &private_normalized_path[0];
445
 
    normalized_path.str= path.str;
446
 
    path.length= normalized_path.length= private_normalized_path.size();
447
 
  }
448
 
}
449
 
 
450
 
 
451
 
/*
452
 
  Used for shares that will go into the cache.
453
 
*/
454
 
TableShare::TableShare(const identifier::Table::Type type_arg,
455
 
                       const identifier::Table &identifier,
456
 
                       char *path_arg,
457
 
                       uint32_t path_length_arg) :
458
 
  table_category(TABLE_UNKNOWN_CATEGORY),
459
 
  found_next_number_field(NULL),
460
 
  timestamp_field(NULL),
461
 
  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),
469
 
  block_size(0),
470
 
  version(0),
471
 
  timestamp_offset(0),
472
 
  reclength(0),
473
 
  stored_rec_length(0),
474
 
  max_rows(0),
475
 
  _table_message(NULL),
476
 
  storage_engine(NULL),
477
 
  tmp_table(type_arg),
478
 
  _ref_count(0),
479
 
  null_bytes(0),
480
 
  last_null_bit_pos(0),
481
 
  _field_size(0),
482
 
  rec_buff_length(0),
483
 
  keys(0),
484
 
  key_parts(0),
485
 
  max_key_length(0),
486
 
  max_unique_length(0),
487
 
  total_key_length(0),
488
 
  uniques(0),
489
 
  null_fields(0),
490
 
  blob_fields(0),
491
 
  has_variable_width(false),
492
 
  db_create_options(0),
493
 
  db_options_in_use(0),
494
 
  db_record_offset(0),
495
 
  rowid_field_offset(0),
496
 
  primary_key(MAX_KEY),
497
 
  next_number_index(0),
498
 
  next_number_key_offset(0),
499
 
  next_number_keypart(0),
500
 
  error(0),
501
 
  open_errno(0),
502
 
  errarg(0),
503
 
  blob_ptr_size(portable_sizeof_char_ptr),
504
 
  db_low_byte_first(false),
505
 
  keys_in_use(0),
506
 
  keys_for_keyread(0)
507
 
{
508
 
  char *path_buff;
 
468
TableDefinitionCache &TableShare::getCache()
 
469
{
 
470
  return table_def_cache;
 
471
}
 
472
 
 
473
TableShare::TableShare(char *key, uint32_t key_length, char *path_arg, uint32_t path_length_arg) :
 
474
  table_category(TABLE_UNKNOWN_CATEGORY),
 
475
  open_count(0),
 
476
  found_next_number_field(NULL),
 
477
  timestamp_field(NULL),
 
478
  key_info(NULL),
 
479
  blob_field(NULL),
 
480
  block_size(0),
 
481
  version(0),
 
482
  timestamp_offset(0),
 
483
  reclength(0),
 
484
  stored_rec_length(0),
 
485
  row_type(ROW_TYPE_DEFAULT),
 
486
  max_rows(0),
 
487
  table_proto(NULL),
 
488
  storage_engine(NULL),
 
489
  tmp_table(message::Table::STANDARD),
 
490
  ref_count(0),
 
491
  null_bytes(0),
 
492
  last_null_bit_pos(0),
 
493
  fields(0),
 
494
  rec_buff_length(0),
 
495
  keys(0),
 
496
  key_parts(0),
 
497
  max_key_length(0),
 
498
  max_unique_length(0),
 
499
  total_key_length(0),
 
500
  uniques(0),
 
501
  null_fields(0),
 
502
  blob_fields(0),
 
503
  timestamp_field_offset(0),
 
504
  varchar_fields(0),
 
505
  db_create_options(0),
 
506
  db_options_in_use(0),
 
507
  db_record_offset(0),
 
508
  rowid_field_offset(0),
 
509
  primary_key(0),
 
510
  next_number_index(0),
 
511
  next_number_key_offset(0),
 
512
  next_number_keypart(0),
 
513
  error(0),
 
514
  open_errno(0),
 
515
  errarg(0),
 
516
  column_bitmap_size(0),
 
517
  blob_ptr_size(0),
 
518
  db_low_byte_first(false),
 
519
  name_lock(false),
 
520
  replace_with_name_lock(false),
 
521
  waiting_on_cond(false),
 
522
  keys_in_use(0),
 
523
  keys_for_keyread(0),
 
524
  event_observers(NULL),
 
525
  newed(true)
 
526
{
 
527
  memset(&name_hash, 0, sizeof(HASH));
 
528
 
 
529
  table_charset= 0;
 
530
  memset(&table_cache_key, 0, sizeof(LEX_STRING));
 
531
  memset(&db, 0, sizeof(LEX_STRING));
 
532
  memset(&table_name, 0, sizeof(LEX_STRING));
 
533
  memset(&path, 0, sizeof(LEX_STRING));
 
534
  memset(&normalized_path, 0, sizeof(LEX_STRING));
 
535
 
 
536
  mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
 
537
  char *key_buff, *path_buff;
509
538
  std::string _path;
510
539
 
511
 
  private_key_for_cache= identifier.getKey();
512
 
  /*
513
 
    Let us use the fact that the key is "db/0/table_name/0" + optional
514
 
    part for temporary tables.
515
 
  */
516
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
517
 
  db.length=         strlen(db.str);
518
 
  table_name.str=    db.str + db.length + 1;
 
540
  db.str= key;
 
541
  db.length= strlen(db.str);
 
542
  table_name.str= db.str + db.length + 1;
519
543
  table_name.length= strlen(table_name.str);
520
544
 
521
545
  if (path_arg)
524
548
  }
525
549
  else
526
550
  {
527
 
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
 
551
    build_table_filename(_path, db.str, table_name.str, false);
528
552
  }
529
553
 
530
 
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
 
554
  if (mem_root.multi_alloc_root(0, &key_buff, key_length,
 
555
                                &path_buff, _path.length() + 1,
 
556
                                NULL))
531
557
  {
 
558
    memcpy(key_buff, key, key_length);
 
559
    set_table_cache_key(key_buff, key_length, db.length, table_name.length);
 
560
 
532
561
    setPath(path_buff, _path.length());
533
562
    strcpy(path_buff, _path.c_str());
534
563
    setNormalizedPath(path_buff, _path.length());
535
564
 
536
565
    version= refresh_version;
 
566
 
 
567
    pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
 
568
    pthread_cond_init(&cond, NULL);
537
569
  }
538
570
  else
539
571
  {
540
572
    assert(0); // We should throw here.
541
 
    abort();
542
573
  }
543
 
}
544
 
 
545
 
void TableShare::init(const char *new_table_name,
546
 
                      const char *new_path)
547
 
{
548
 
 
549
 
  table_category=         TABLE_CATEGORY_TEMPORARY;
550
 
  tmp_table=              message::Table::INTERNAL;
551
 
  db.str= (char *)"";
552
 
  db.length= 0;
553
 
  table_name.str=         (char*) new_table_name;
554
 
  table_name.length=      strlen(new_table_name);
555
 
  path.str=               (char*) new_path;
556
 
  normalized_path.str=    (char*) new_path;
557
 
  path.length= normalized_path.length= strlen(new_path);
558
 
}
559
 
 
560
 
TableShare::~TableShare() 
561
 
{
562
 
  storage_engine= NULL;
563
 
 
564
 
  mem_root.free_root(MYF(0));                 // Free's share
565
 
}
566
 
 
567
 
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
568
 
{
569
 
  private_key_for_cache= identifier_arg.getKey();
570
 
 
571
 
  /*
572
 
    Let us use the fact that the key is "db/0/table_name/0" + optional
573
 
    part for temporary tables.
574
 
  */
575
 
  db.str= const_cast<char *>(private_key_for_cache.vector());
576
 
  db.length=         strlen(db.str);
577
 
  table_name.str=    db.str + db.length + 1;
578
 
  table_name.length= strlen(table_name.str);
579
 
 
580
 
  getTableMessage()->set_name(identifier_arg.getTableName());
581
 
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
582
 
}
583
 
 
584
 
bool TableShare::parse_table_proto(Session& session, message::Table &table)
585
 
{
586
 
  drizzled::error_t local_error= EE_OK;
 
574
 
 
575
  newed= true;
 
576
}
 
577
 
 
578
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
 
579
{
 
580
  int local_error= 0;
587
581
 
588
582
  if (! table.IsInitialized())
589
583
  {
590
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
591
 
             table.name().empty() ? " " :  table.name().c_str(),
592
 
             table.InitializationErrorString().c_str());
593
 
 
 
584
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
585
    return ER_CORRUPT_TABLE_DEFINITION;
595
586
  }
596
587
 
597
 
  setTableMessage(table);
 
588
  setTableProto(new(nothrow) message::Table(table));
598
589
 
599
590
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
591
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
615
606
  db_create_options= (local_db_create_options & 0x0000FFFF);
616
607
  db_options_in_use= db_create_options;
617
608
 
 
609
  row_type= table_options.has_row_type() ?
 
610
    (enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
 
611
 
618
612
  block_size= table_options.has_block_size() ?
619
613
    table_options.block_size() : 0;
620
614
 
621
 
  table_charset= get_charset(table_options.collation_id());
 
615
  table_charset= get_charset(table_options.has_collation_id()?
 
616
                                    table_options.collation_id() : 0);
622
617
 
623
 
  if (not table_charset)
 
618
  if (!table_charset)
624
619
  {
625
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
626
 
             table_options.collation().c_str(),
627
 
             table.name().c_str());
628
 
 
629
 
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
 
620
    /* unknown charset in head[38] or pre-3.23 frm */
 
621
    if (use_mb(default_charset_info))
 
622
    {
 
623
      /* Warn that we may be changing the size of character columns */
 
624
      errmsg_printf(ERRMSG_LVL_WARN,
 
625
                    _("'%s' had no or invalid character set, "
 
626
                      "and default character set is multi-byte, "
 
627
                      "so character column sizes may have changed"),
 
628
                    getPath());
 
629
    }
 
630
    table_charset= default_charset_info;
630
631
  }
631
632
 
632
633
  db_record_offset= 1;
633
634
 
 
635
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
 
636
 
634
637
  keys= table.indexes_size();
635
638
 
636
639
  key_parts= 0;
736
739
 
737
740
      key_part->length= part.compare_length();
738
741
 
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
742
      key_part->store_length= key_part->length;
758
743
 
759
744
      /* key_part->offset is set later */
780
765
  keys_for_keyread.reset();
781
766
  set_prefix(keys_in_use, keys);
782
767
 
783
 
  _field_size= table.field_size();
 
768
  fields= table.field_size();
784
769
 
785
 
  setFields(_field_size + 1);
786
 
  _fields[_field_size]= NULL;
 
770
  setFields(fields + 1);
 
771
  field[fields]= NULL;
787
772
 
788
773
  uint32_t local_null_fields= 0;
789
774
  reclength= 0;
790
775
 
791
 
  std::vector<uint32_t> field_offsets;
792
 
  std::vector<uint32_t> field_pack_length;
 
776
  vector<uint32_t> field_offsets;
 
777
  vector<uint32_t> field_pack_length;
793
778
 
794
 
  field_offsets.resize(_field_size);
795
 
  field_pack_length.resize(_field_size);
 
779
  field_offsets.resize(fields);
 
780
  field_pack_length.resize(fields);
796
781
 
797
782
  uint32_t interval_count= 0;
798
783
  uint32_t interval_parts= 0;
799
784
 
800
785
  uint32_t stored_columns_reclength= 0;
801
786
 
802
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
787
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
803
788
  {
804
789
    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
 
    }
 
790
    if (pfield.constraints().is_nullable())
 
791
      local_null_fields++;
813
792
 
814
 
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
 
793
    enum_field_types drizzle_field_type=
 
794
      proto_field_type_to_drizzle_type(pfield.type());
815
795
 
816
796
    field_offsets[fieldnr]= stored_columns_reclength;
817
797
 
840
820
      {
841
821
        message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
842
822
 
843
 
        field_pack_length[fieldnr]= 4;
 
823
        field_pack_length[fieldnr]=
 
824
          get_enum_pack_length(field_options.field_value_size());
844
825
 
845
826
        interval_count++;
846
827
        interval_parts+= field_options.field_value_size();
850
831
      {
851
832
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
852
833
 
853
 
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
 
834
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
854
835
      }
855
836
      break;
856
837
    default:
900
881
 
901
882
  uint32_t interval_nr= 0;
902
883
 
903
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
884
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
904
885
  {
905
886
    message::Table::Field pfield= table.field(fieldnr);
906
887
 
912
893
 
913
894
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
914
895
    {
915
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
 
896
      char errmsg[100];
 
897
      snprintf(errmsg, sizeof(errmsg),
 
898
               _("ENUM column %s has greater than %d possible values"),
 
899
               pfield.name().c_str(),
 
900
               Field_enum::max_supported_elements);
 
901
      errmsg[99]='\0';
916
902
 
917
 
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
 
903
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
 
904
      return ER_CORRUPT_TABLE_DEFINITION;
918
905
    }
919
906
 
920
907
 
957
944
  /* and read the fields */
958
945
  interval_nr= 0;
959
946
 
960
 
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
 
947
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
 
948
 
 
949
  if (use_hash)
 
950
    use_hash= ! hash_init(&name_hash,
 
951
                          system_charset_info,
 
952
                          fields,
 
953
                          0,
 
954
                          0,
 
955
                          (hash_get_key) get_field_name,
 
956
                          0,
 
957
                          0);
961
958
 
962
959
  unsigned char* null_pos= getDefaultValues();
963
960
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
964
961
 
965
 
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
 
962
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
966
963
  {
967
964
    message::Table::Field pfield= table.field(fieldnr);
968
965
 
975
972
    }
976
973
 
977
974
    if (pfield.has_options() &&
978
 
        pfield.options().has_default_expression() &&
979
 
        pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
 
975
        pfield.options().has_default_value() &&
 
976
        pfield.options().default_value().compare("NOW()") == 0)
980
977
    {
981
 
      if (pfield.options().has_update_expression() &&
982
 
          pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
978
      if (pfield.options().has_update_value() &&
 
979
          pfield.options().update_value().compare("NOW()") == 0)
983
980
      {
984
981
        unireg_type= Field::TIMESTAMP_DNUN_FIELD;
985
982
      }
986
 
      else if (! pfield.options().has_update_expression())
 
983
      else if (! pfield.options().has_update_value())
987
984
      {
988
985
        unireg_type= Field::TIMESTAMP_DN_FIELD;
989
986
      }
990
987
      else
991
 
      {
992
 
        assert(0); // Invalid update value.
993
 
        abort();
994
 
      }
 
988
        assert(1); // Invalid update value.
995
989
    }
996
990
    else if (pfield.has_options() &&
997
 
             pfield.options().has_update_expression() &&
998
 
             pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
 
991
             pfield.options().has_update_value() &&
 
992
             pfield.options().update_value().compare("NOW()") == 0)
999
993
    {
1000
994
      unireg_type= Field::TIMESTAMP_UN_FIELD;
1001
995
    }
1017
1011
 
1018
1012
    enum_field_types field_type;
1019
1013
 
1020
 
    field_type= proto_field_type_to_drizzle_type(pfield);
 
1014
    field_type= proto_field_type_to_drizzle_type(pfield.type());
1021
1015
 
1022
1016
    const CHARSET_INFO *charset= &my_charset_bin;
1023
1017
 
1062
1056
      {
1063
1057
        if (fo.scale() > DECIMAL_MAX_SCALE)
1064
1058
        {
1065
 
          local_error= ER_NOT_FORM_FILE;
 
1059
          local_error= 4;
1066
1060
 
1067
 
          return true;
 
1061
          return local_error;
1068
1062
        }
1069
1063
        decimals= static_cast<uint8_t>(fo.scale());
1070
1064
      }
1073
1067
    Item *default_value= NULL;
1074
1068
 
1075
1069
    if (pfield.options().has_default_value() ||
1076
 
        pfield.options().default_null()  ||
 
1070
        pfield.options().has_default_null()  ||
1077
1071
        pfield.options().has_default_bin_value())
1078
1072
    {
1079
1073
      default_value= default_value_item(field_type,
1084
1078
    }
1085
1079
 
1086
1080
 
 
1081
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
 
1082
    memset(&temp_table, 0, sizeof(temp_table));
 
1083
    temp_table.setShare(this);
 
1084
    temp_table.in_use= &session;
 
1085
    temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
 
1086
    temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
 
1087
 
1087
1088
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1088
1089
 
1089
 
    // We set field_length in this loop.
1090
1090
    switch (field_type)
1091
1091
    {
1092
1092
    case DRIZZLE_TYPE_BLOB:
1118
1118
            decimals != NOT_FIXED_DEC)
1119
1119
        {
1120
1120
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1121
 
          local_error= ER_M_BIGGER_THAN_D;
1122
 
          return true;
 
1121
          local_error= 1;
 
1122
 
 
1123
          return local_error;
1123
1124
        }
1124
1125
        break;
1125
1126
      }
1127
1128
      {
1128
1129
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1129
1130
 
1130
 
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1131
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1131
1132
                                                     false);
1132
1133
        break;
1133
1134
      }
 
1135
    case DRIZZLE_TYPE_TIMESTAMP:
1134
1136
    case DRIZZLE_TYPE_DATETIME:
1135
1137
      field_length= DateTime::MAX_STRING_LENGTH;
1136
1138
      break;
1163
1165
      }
1164
1166
      break;
1165
1167
    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();
 
1168
      field_length= MAX_BIGINT_WIDTH;
1185
1169
      break;
1186
1170
    case DRIZZLE_TYPE_NULL:
1187
1171
      abort(); // Programming error
1188
1172
    }
1189
1173
 
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;
 
1174
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
1175
                                field_length,
 
1176
                                pfield.constraints().is_nullable(),
 
1177
                                null_pos,
 
1178
                                null_bit_pos,
 
1179
                                decimals,
 
1180
                                field_type,
 
1181
                                charset,
 
1182
                                (Field::utype) MTYP_TYPENR(unireg_type),
 
1183
                                ((field_type == DRIZZLE_TYPE_ENUM) ?
 
1184
                                 &intervals[interval_nr++]
 
1185
                                 : (TYPELIB*) 0),
 
1186
                                getTableProto()->field(fieldnr).name().c_str());
 
1187
 
 
1188
    field[fieldnr]= f;
1241
1189
 
1242
1190
    f->init(&temp_table); /* blob default values need table obj */
1243
1191
 
1252
1200
    if (default_value)
1253
1201
    {
1254
1202
      enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1255
 
      session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
 
1203
      session.count_cuted_fields= CHECK_FIELD_WARN;
1256
1204
      int res= default_value->save_in_field(f, 1);
1257
1205
      session.count_cuted_fields= old_count_cuted_fields;
1258
1206
      if (res != 0 && res != 3) /* @TODO Huh? */
1259
1207
      {
1260
1208
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1261
 
        local_error= ER_INVALID_DEFAULT;
 
1209
        local_error= 1;
1262
1210
 
1263
 
        return true;
 
1211
        return local_error;
1264
1212
      }
1265
1213
    }
1266
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
 
1214
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
 
1215
             (f->flags & NOT_NULL_FLAG))
1267
1216
    {
1268
1217
      f->set_notnull();
1269
1218
      f->store((int64_t) 1, true);
1274
1223
    }
1275
1224
 
1276
1225
    /* hack to undo f->init() */
1277
 
    f->setTable(NULL);
 
1226
    f->table= NULL;
1278
1227
    f->orig_table= NULL;
1279
1228
 
1280
 
    f->setPosition(fieldnr);
 
1229
    f->field_index= fieldnr;
1281
1230
    f->comment= comment;
1282
 
    if (not default_value &&
1283
 
        not (f->unireg_check==Field::NEXT_NUMBER) &&
 
1231
    if (! default_value &&
 
1232
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
1284
1233
        (f->flags & NOT_NULL_FLAG) &&
1285
 
        (not f->is_timestamp()))
 
1234
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1286
1235
    {
1287
1236
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1288
1237
    }
1289
1238
 
1290
1239
    if (f->unireg_check == Field::NEXT_NUMBER)
1291
 
      found_next_number_field= &(_fields[fieldnr]);
 
1240
      found_next_number_field= &(field[fieldnr]);
 
1241
 
 
1242
    if (timestamp_field == f)
 
1243
      timestamp_field_offset= fieldnr;
1292
1244
 
1293
1245
    if (use_hash) /* supposedly this never fails... but comments lie */
1294
 
    {
1295
 
      const char *local_field_name= _fields[fieldnr]->field_name;
1296
 
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1297
 
    }
 
1246
      (void) my_hash_insert(&name_hash,
 
1247
                            (unsigned char*)&(field[fieldnr]));
 
1248
 
1298
1249
  }
1299
1250
 
1300
1251
  keyinfo= key_info;
1319
1270
    We need to set the unused bits to 1. If the number of bits is a multiple
1320
1271
    of 8 there are no unused bits.
1321
1272
  */
 
1273
 
1322
1274
  if (null_count & 7)
1323
1275
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1324
1276
 
1349
1301
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1350
1302
        {
1351
1303
          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)
 
1304
          if (! fieldnr ||
 
1305
              field[fieldnr-1]->null_ptr ||
 
1306
              field[fieldnr-1]->key_length() != key_part[i].length)
1355
1307
          {
1356
1308
            local_primary_key= MAX_KEY; // Can't be used
1357
1309
            break;
1366
1318
        {
1367
1319
          return ENOMEM;
1368
1320
        }
1369
 
        local_field= key_part->field= _fields[key_part->fieldnr-1];
 
1321
        local_field= key_part->field= field[key_part->fieldnr-1];
1370
1322
        key_part->type= local_field->key_type();
1371
1323
        if (local_field->null_ptr)
1372
1324
        {
1460
1412
        }
1461
1413
      }
1462
1414
    }
 
1415
    else
 
1416
    {
 
1417
      primary_key = MAX_KEY; // we do not have a primary key
 
1418
    }
 
1419
  }
 
1420
  else
 
1421
  {
 
1422
    primary_key= MAX_KEY;
1463
1423
  }
1464
1424
 
1465
1425
  if (found_next_number_field)
1472
1432
                            &next_number_keypart)) < 0)
1473
1433
    {
1474
1434
      /* Wrong field definition */
1475
 
      local_error= ER_NOT_FORM_FILE;
 
1435
      local_error= 4;
1476
1436
 
1477
 
      return true;
 
1437
      return local_error;
1478
1438
    }
1479
1439
    else
1480
1440
    {
1484
1444
 
1485
1445
  if (blob_fields)
1486
1446
  {
 
1447
    uint32_t k, *save;
 
1448
 
1487
1449
    /* Store offsets to blob fields to find them fast */
1488
1450
    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++)
 
1451
    save= &blob_field[0];
 
1452
    k= 0;
 
1453
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1492
1454
    {
1493
1455
      if ((*iter)->flags & BLOB_FLAG)
1494
1456
        (*save++)= k;
1495
1457
    }
1496
1458
  }
1497
1459
 
1498
 
  all_set.clear();
1499
 
  all_set.resize(_field_size);
1500
 
  all_set.set();
1501
 
 
1502
 
  return local_error != EE_OK;
1503
 
}
 
1460
  db_low_byte_first= true; // @todo Question this.
 
1461
  column_bitmap_size= bitmap_buffer_size(fields);
 
1462
 
 
1463
  all_bitmap.resize(column_bitmap_size);
 
1464
  all_set.init(&all_bitmap[0], fields);
 
1465
  all_set.setAll();
 
1466
 
 
1467
  return local_error;
 
1468
}
 
1469
 
 
1470
int TableShare::parse_table_proto(Session& session, message::Table &table)
 
1471
{
 
1472
  int local_error= inner_parse_table_proto(session, table);
 
1473
 
 
1474
  if (not local_error)
 
1475
    return 0;
 
1476
 
 
1477
  error= local_error;
 
1478
  open_errno= errno;
 
1479
  errarg= 0;
 
1480
  hash_free(&name_hash);
 
1481
  open_table_error(local_error, open_errno, 0);
 
1482
 
 
1483
  return local_error;
 
1484
}
 
1485
 
1504
1486
 
1505
1487
/*
1506
1488
  Read table definition from a binary / text based .frm cursor
1512
1494
 
1513
1495
  NOTES
1514
1496
  This function is called when the table definition is not cached in
1515
 
  definition::Cache::singleton().getCache()
 
1497
  table_def_cache
1516
1498
  The data is returned in 'share', which is alloced by
1517
1499
  alloc_table_share().. The code assumes that share is initialized.
1518
1500
 
1526
1508
  6    Unknown .frm version
1527
1509
*/
1528
1510
 
1529
 
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
 
1511
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1530
1512
{
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())
 
1513
  int local_error;
 
1514
  bool error_given;
 
1515
 
 
1516
  local_error= 1;
 
1517
  error_given= 0;
 
1518
 
 
1519
  message::Table table;
 
1520
 
 
1521
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
 
1522
 
 
1523
  if (local_error != EEXIST)
1536
1524
  {
1537
 
    if (parse_table_proto(session, *table))
 
1525
    if (local_error > 0)
1538
1526
    {
1539
 
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
 
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1527
      errno= local_error;
 
1528
      local_error= 1;
1541
1529
    }
1542
1530
    else
1543
1531
    {
1544
 
      setTableCategory(TABLE_CATEGORY_USER);
1545
 
      local_error= EE_OK;
 
1532
      if (not table.IsInitialized())
 
1533
      {
 
1534
        local_error= 4;
 
1535
      }
1546
1536
    }
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);
 
1537
    goto err_not_open;
 
1538
  }
 
1539
 
 
1540
  local_error= parse_table_proto(session, table);
 
1541
 
 
1542
  setTableCategory(TABLE_CATEGORY_USER);
 
1543
 
 
1544
err_not_open:
 
1545
  if (local_error && !error_given)
 
1546
  {
 
1547
    error= local_error;
 
1548
    open_table_error(error, (open_errno= errno), 0);
 
1549
  }
 
1550
 
 
1551
  return(error);
1560
1552
}
1561
1553
 
1562
1554
 
1583
1575
  5    Error (see open_table_error: charset unavailable)
1584
1576
  7    Table definition has changed in engine
1585
1577
*/
1586
 
int TableShare::open_table_from_share(Session *session,
1587
 
                                      const identifier::Table &identifier,
1588
 
                                      const char *alias,
 
1578
 
 
1579
int TableShare::open_table_from_share(Session *session, const char *alias,
1589
1580
                                      uint32_t db_stat, uint32_t ha_open_flags,
1590
1581
                                      Table &outparam)
1591
1582
{
 
1583
  int local_error;
 
1584
  uint32_t records, bitmap_size;
1592
1585
  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;
 
1586
  unsigned char *record, *bitmaps;
1621
1587
  Field **field_ptr;
1622
1588
 
 
1589
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
1590
  assert(session->lex->is_lex_started);
 
1591
 
1623
1592
  local_error= 1;
1624
1593
  outparam.resetTable(session, this, db_stat);
1625
1594
 
1626
 
  outparam.setAlias(alias);
 
1595
 
 
1596
  if (not (outparam.alias= strdup(alias)))
 
1597
    goto err;
1627
1598
 
1628
1599
  /* Allocate Cursor */
1629
 
  if (not (outparam.cursor= db_type()->getCursor(outparam)))
1630
 
    return local_error;
 
1600
  if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
 
1601
    goto err;
1631
1602
 
1632
1603
  local_error= 4;
1633
1604
  records= 0;
1637
1608
  records++;
1638
1609
 
1639
1610
  if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1640
 
    return local_error;
 
1611
    goto err;
1641
1612
 
1642
1613
  if (records == 0)
1643
1614
  {
1650
1621
    if (records > 1)
1651
1622
      outparam.record[1]= record+ rec_buff_length;
1652
1623
    else
1653
 
      outparam.record[1]= outparam.getInsertRecord();   // Safety
 
1624
      outparam.record[1]= outparam.record[0];   // Safety
1654
1625
  }
1655
1626
 
1656
 
#ifdef HAVE_VALGRIND
 
1627
#ifdef HAVE_purify
1657
1628
  /*
1658
1629
    We need this because when we read var-length rows, we are not updating
1659
1630
    bytes after end of varchar
1660
1631
  */
1661
1632
  if (records > 1)
1662
1633
  {
1663
 
    memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1664
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1634
    memcpy(outparam.record[0], getDefaultValues(), rec_buff_length);
 
1635
    memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1665
1636
    if (records > 2)
1666
 
      memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
 
1637
      memcpy(outparam.record[1], getDefaultValues(), rec_buff_length);
1667
1638
  }
1668
1639
#endif
1669
1640
  if (records > 1)
1670
1641
  {
1671
 
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
 
1642
    memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1672
1643
  }
1673
1644
 
1674
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
 
1645
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1675
1646
  {
1676
 
    return local_error;
 
1647
    goto err;
1677
1648
  }
1678
1649
 
1679
1650
  outparam.setFields(field_ptr);
1680
1651
 
1681
 
  record= (unsigned char*) outparam.getInsertRecord()-1;        /* Fieldstart = 1 */
 
1652
  record= (unsigned char*) outparam.record[0]-1;        /* Fieldstart = 1 */
1682
1653
 
1683
1654
  outparam.null_flags= (unsigned char*) record+1;
1684
1655
 
1685
1656
  /* 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++)
 
1657
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1687
1658
  {
1688
 
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1689
 
      return local_error;
 
1659
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1660
      goto err;
1690
1661
  }
1691
1662
  (*field_ptr)= 0;                              // End marker
1692
1663
 
1694
1665
    outparam.found_next_number_field=
1695
1666
      outparam.getField(positionFields(found_next_number_field));
1696
1667
  if (timestamp_field)
1697
 
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
 
1668
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
 
1669
 
1698
1670
 
1699
1671
  /* Fix key->name and key_part->field */
1700
1672
  if (key_parts)
1704
1676
    uint32_t n_length;
1705
1677
    n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1706
1678
    if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1707
 
      return local_error;
 
1679
      goto err;
1708
1680
    outparam.key_info= local_key_info;
1709
1681
    key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1710
1682
 
1743
1715
 
1744
1716
  /* Allocate bitmaps */
1745
1717
 
1746
 
  outparam.def_read_set.resize(_field_size);
1747
 
  outparam.def_write_set.resize(_field_size);
1748
 
  outparam.tmp_set.resize(_field_size);
 
1718
  bitmap_size= column_bitmap_size;
 
1719
  if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
 
1720
  {
 
1721
    goto err;
 
1722
  }
 
1723
  outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
 
1724
  outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
 
1725
  outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1749
1726
  outparam.default_column_bitmaps();
1750
1727
 
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
1728
  /* The table struct is now initialized;  Open the table */
1760
 
  int local_error= 2;
 
1729
  local_error= 2;
1761
1730
  if (db_stat)
1762
1731
  {
1763
1732
    assert(!(db_stat & HA_WAIT_IF_LOCKED));
1764
1733
    int ha_err;
1765
 
 
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))))
 
1734
    if ((ha_err= (outparam.cursor->
 
1735
                  ha_open(&outparam, getNormalizedPath(),
 
1736
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
 
1737
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1769
1738
    {
1770
1739
      switch (ha_err)
1771
1740
      {
1792
1761
          local_error= 7;
1793
1762
        break;
1794
1763
      }
1795
 
      return local_error;
 
1764
      goto err;
1796
1765
    }
1797
1766
  }
1798
1767
 
 
1768
#if defined(HAVE_purify)
 
1769
  memset(bitmaps, 0, bitmap_size*3);
 
1770
#endif
 
1771
 
1799
1772
  return 0;
 
1773
 
 
1774
err:
 
1775
  if (!error_reported)
 
1776
    open_table_error(local_error, errno, 0);
 
1777
 
 
1778
  delete outparam.cursor;
 
1779
  outparam.cursor= 0;                           // For easier error checking
 
1780
  outparam.db_stat= 0;
 
1781
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
 
1782
  free((char*) outparam.alias);
 
1783
  return (local_error);
1800
1784
}
1801
1785
 
1802
1786
/* error message when opening a form cursor */
1803
1787
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1804
1788
{
 
1789
  int err_no;
1805
1790
  char buff[FN_REFLEN];
1806
1791
  myf errortype= ME_ERROR+ME_WAITTANG;
1807
1792
 
1810
1795
  case 1:
1811
1796
    if (db_errno == ENOENT)
1812
1797
    {
1813
 
      identifier::Table identifier(db.str, table_name.str);
1814
 
      my_error(ER_TABLE_UNKNOWN, identifier);
 
1798
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1815
1799
    }
1816
1800
    else
1817
1801
    {
1822
1806
    break;
1823
1807
  case 2:
1824
1808
    {
1825
 
      drizzled::error_t err_no;
1826
 
 
1827
1809
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1828
1810
        ER_FILE_USED : ER_CANT_OPEN_FILE;
1829
 
 
1830
1811
      my_error(err_no, errortype, normalized_path.str, db_errno);
1831
1812
      break;
1832
1813
    }
1862
1843
  return;
1863
1844
} /* open_table_error */
1864
1845
 
1865
 
Field *TableShare::make_field(const message::Table::Field &pfield,
1866
 
                              unsigned char *ptr,
 
1846
Field *TableShare::make_field(unsigned char *ptr,
1867
1847
                              uint32_t field_length,
1868
1848
                              bool is_nullable,
1869
1849
                              unsigned char *null_pos,
1875
1855
                              TYPELIB *interval,
1876
1856
                              const char *field_name)
1877
1857
{
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
1858
  if (! is_nullable)
1908
1859
  {
1909
1860
    null_pos=0;
1914
1865
    null_bit= ((unsigned char) 1) << null_bit;
1915
1866
  }
1916
1867
 
 
1868
  switch (field_type) 
 
1869
  {
 
1870
  case DRIZZLE_TYPE_DATE:
 
1871
  case DRIZZLE_TYPE_DATETIME:
 
1872
  case DRIZZLE_TYPE_TIMESTAMP:
 
1873
    field_charset= &my_charset_bin;
 
1874
  default: break;
 
1875
  }
 
1876
 
1917
1877
  switch (field_type)
1918
1878
  {
1919
1879
  case DRIZZLE_TYPE_ENUM:
1920
1880
    return new (&mem_root) Field_enum(ptr,
1921
 
                                      field_length,
1922
 
                                      null_pos,
1923
 
                                      null_bit,
1924
 
                                      field_name,
1925
 
                                      interval,
1926
 
                                      field_charset);
 
1881
                                 field_length,
 
1882
                                 null_pos,
 
1883
                                 null_bit,
 
1884
                                 field_name,
 
1885
                                 get_enum_pack_length(interval->count),
 
1886
                                 interval,
 
1887
                                 field_charset);
1927
1888
  case DRIZZLE_TYPE_VARCHAR:
1928
 
    setVariableWidth();
1929
1889
    return new (&mem_root) Field_varstring(ptr,field_length,
1930
 
                                      ha_varchar_packlength(field_length),
 
1890
                                      HA_VARCHAR_PACKLENGTH(field_length),
1931
1891
                                      null_pos,null_bit,
1932
1892
                                      field_name,
 
1893
                                      this,
1933
1894
                                      field_charset);
1934
1895
  case DRIZZLE_TYPE_BLOB:
1935
1896
    return new (&mem_root) Field_blob(ptr,
1936
 
                                      null_pos,
1937
 
                                      null_bit,
1938
 
                                      field_name,
1939
 
                                      this,
1940
 
                                      field_charset);
 
1897
                                 null_pos,
 
1898
                                 null_bit,
 
1899
                                 field_name,
 
1900
                                 this,
 
1901
                                 calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
1902
                                 field_charset);
1941
1903
  case DRIZZLE_TYPE_DECIMAL:
1942
1904
    return new (&mem_root) Field_decimal(ptr,
1943
 
                                         field_length,
1944
 
                                         null_pos,
1945
 
                                         null_bit,
1946
 
                                         unireg_check,
1947
 
                                         field_name,
1948
 
                                         decimals);
 
1905
                                    field_length,
 
1906
                                    null_pos,
 
1907
                                    null_bit,
 
1908
                                    unireg_check,
 
1909
                                    field_name,
 
1910
                                    decimals,
 
1911
                                    false,
 
1912
                                    false /* is_unsigned */);
1949
1913
  case DRIZZLE_TYPE_DOUBLE:
1950
1914
    return new (&mem_root) Field_double(ptr,
1951
1915
                                   field_length,
1956
1920
                                   decimals,
1957
1921
                                   false,
1958
1922
                                   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
1923
  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);
 
1924
    return new (&mem_root) Field_long(ptr,
 
1925
                                 field_length,
 
1926
                                 null_pos,
 
1927
                                 null_bit,
 
1928
                                 unireg_check,
 
1929
                                 field_name,
 
1930
                                 false,
 
1931
                                 false /* is_unsigned */);
1979
1932
  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);
 
1933
    return new (&mem_root) Field_int64_t(ptr,
 
1934
                                    field_length,
 
1935
                                    null_pos,
 
1936
                                    null_bit,
 
1937
                                    unireg_check,
 
1938
                                    field_name,
 
1939
                                    false,
 
1940
                                    false /* is_unsigned */);
2005
1941
  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);
 
1942
    return new (&mem_root) Field_timestamp(ptr,
 
1943
                                      field_length,
 
1944
                                      null_pos,
 
1945
                                      null_bit,
 
1946
                                      unireg_check,
 
1947
                                      field_name,
 
1948
                                      this,
 
1949
                                      field_charset);
2018
1950
  case DRIZZLE_TYPE_DATE:
2019
1951
    return new (&mem_root) Field_date(ptr,
2020
1952
                                 null_pos,
2021
1953
                                 null_bit,
2022
 
                                 field_name);
 
1954
                                 field_name,
 
1955
                                 field_charset);
2023
1956
  case DRIZZLE_TYPE_DATETIME:
2024
1957
    return new (&mem_root) Field_datetime(ptr,
2025
1958
                                     null_pos,
2026
1959
                                     null_bit,
2027
 
                                     field_name);
 
1960
                                     field_name,
 
1961
                                     field_charset);
2028
1962
  case DRIZZLE_TYPE_NULL:
2029
1963
    return new (&mem_root) Field_null(ptr,
2030
 
                                      field_length,
2031
 
                                      field_name);
 
1964
                                 field_length,
 
1965
                                 field_name,
 
1966
                                 field_charset);
 
1967
  default: // Impossible (Wrong version)
 
1968
    break;
2032
1969
  }
2033
 
  assert(0);
2034
 
  abort();
2035
 
}
2036
 
 
2037
 
void TableShare::refreshVersion()
2038
 
{
2039
 
  version= refresh_version;
 
1970
  return 0;
2040
1971
}
2041
1972
 
2042
1973