~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2011-02-17 10:09:00 UTC
  • mfrom: (2173.2.1 clean-include-usuage)
  • Revision ID: brian@tangent.org-20110217100900-4tpuxxzdl1sj00sh
Merge Monty for headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
6
6
 *
7
7
 *  This program is free software; you can redistribute it and/or modify
8
8
 *  it under the terms of the GNU General Public License as published by
25
25
*/
26
26
 
27
27
/* Basic functions needed by many modules */
28
 
#include "config.h"
 
28
#include <config.h>
29
29
 
30
30
#include <pthread.h>
31
31
#include <float.h>
37
37
 
38
38
#include <cassert>
39
39
 
40
 
#include "drizzled/error.h"
41
 
#include "drizzled/gettext.h"
42
 
#include "drizzled/sql_base.h"
43
 
#include "drizzled/pthread_globals.h"
44
 
#include "drizzled/internal/my_pthread.h"
45
 
#include "drizzled/plugin/event_observer.h"
46
 
 
47
 
#include "drizzled/table.h"
48
 
#include "drizzled/table/shell.h"
49
 
 
50
 
#include "drizzled/session.h"
51
 
 
52
 
#include "drizzled/charset.h"
53
 
#include "drizzled/internal/m_string.h"
54
 
#include "drizzled/internal/my_sys.h"
55
 
 
56
 
#include "drizzled/item/string.h"
57
 
#include "drizzled/item/int.h"
58
 
#include "drizzled/item/decimal.h"
59
 
#include "drizzled/item/float.h"
60
 
#include "drizzled/item/null.h"
61
 
#include "drizzled/temporal.h"
62
 
 
63
 
#include "drizzled/field.h"
64
 
#include "drizzled/field/str.h"
65
 
#include "drizzled/field/num.h"
66
 
#include "drizzled/field/blob.h"
67
 
#include "drizzled/field/enum.h"
68
 
#include "drizzled/field/null.h"
69
 
#include "drizzled/field/date.h"
70
 
#include "drizzled/field/decimal.h"
71
 
#include "drizzled/field/real.h"
72
 
#include "drizzled/field/double.h"
73
 
#include "drizzled/field/int32.h"
74
 
#include "drizzled/field/int64.h"
75
 
#include "drizzled/field/num.h"
76
 
#include "drizzled/field/timestamp.h"
77
 
#include "drizzled/field/datetime.h"
78
 
#include "drizzled/field/varstring.h"
79
 
#include "drizzled/field/uuid.h"
80
 
 
81
 
#include "drizzled/definition/cache.h"
 
40
#include <drizzled/error.h>
 
41
#include <drizzled/gettext.h>
 
42
#include <drizzled/sql_base.h>
 
43
#include <drizzled/pthread_globals.h>
 
44
#include <drizzled/internal/my_pthread.h>
 
45
 
 
46
#include <drizzled/table.h>
 
47
#include <drizzled/table/shell.h>
 
48
 
 
49
#include <drizzled/session.h>
 
50
 
 
51
#include <drizzled/charset.h>
 
52
#include <drizzled/internal/m_string.h>
 
53
#include <drizzled/internal/my_sys.h>
 
54
 
 
55
#include <drizzled/item/string.h>
 
56
#include <drizzled/item/int.h>
 
57
#include <drizzled/item/decimal.h>
 
58
#include <drizzled/item/float.h>
 
59
#include <drizzled/item/null.h>
 
60
#include <drizzled/temporal.h>
 
61
 
 
62
#include <drizzled/field.h>
 
63
#include <drizzled/field/str.h>
 
64
#include <drizzled/field/num.h>
 
65
#include <drizzled/field/blob.h>
 
66
#include <drizzled/field/boolean.h>
 
67
#include <drizzled/field/enum.h>
 
68
#include <drizzled/field/null.h>
 
69
#include <drizzled/field/date.h>
 
70
#include <drizzled/field/decimal.h>
 
71
#include <drizzled/field/real.h>
 
72
#include <drizzled/field/double.h>
 
73
#include <drizzled/field/int32.h>
 
74
#include <drizzled/field/int64.h>
 
75
#include <drizzled/field/size.h>
 
76
#include <drizzled/field/num.h>
 
77
#include <drizzled/field/time.h>
 
78
#include <drizzled/field/epoch.h>
 
79
#include <drizzled/field/datetime.h>
 
80
#include <drizzled/field/microtime.h>
 
81
#include <drizzled/field/varstring.h>
 
82
#include <drizzled/field/uuid.h>
 
83
 
 
84
#include <drizzled/plugin/storage_engine.h>
 
85
 
 
86
#include <drizzled/definition/cache.h>
 
87
#include <drizzled/typelib.h>
 
88
 
 
89
#include <drizzled/refresh_version.h>
82
90
 
83
91
using namespace std;
84
92
 
87
95
 
88
96
extern size_t table_def_size;
89
97
 
90
 
/*****************************************************************************
91
 
  Functions to handle table definition cach (TableShare)
92
 
 *****************************************************************************/
93
 
 
94
 
/*
95
 
  Mark that we are not using table share anymore.
96
 
 
97
 
  SYNOPSIS
98
 
  release()
99
 
  share         Table share
100
 
 
101
 
  IMPLEMENTATION
102
 
  If ref_count goes to zero and (we have done a refresh or if we have
103
 
  already too many open table shares) then delete the definition.
104
 
*/
105
 
 
106
 
void TableShare::release(TableShare *share)
107
 
{
108
 
  bool to_be_deleted= false;
109
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
110
 
 
111
 
  share->lock();
112
 
  if (!--share->ref_count)
113
 
  {
114
 
    to_be_deleted= true;
115
 
  }
116
 
  share->unlock();
117
 
 
118
 
  if (to_be_deleted)
119
 
  {
120
 
    definition::Cache::singleton().erase(share->getCacheKey());
121
 
  }
122
 
}
123
 
 
124
 
void TableShare::release(TableShare::shared_ptr &share)
125
 
{
126
 
  bool to_be_deleted= false;
127
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
128
 
 
129
 
  share->lock();
130
 
  if (!--share->ref_count)
131
 
  {
132
 
    to_be_deleted= true;
133
 
  }
134
 
  share->unlock();
135
 
 
136
 
  if (to_be_deleted)
137
 
  {
138
 
    definition::Cache::singleton().erase(share->getCacheKey());
139
 
  }
140
 
}
141
 
 
142
 
void TableShare::release(const TableIdentifier &identifier)
143
 
{
144
 
  TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
145
 
  if (share)
146
 
  {
147
 
    share->version= 0;                          // Mark for delete
148
 
    if (share->ref_count == 0)
149
 
    {
150
 
      definition::Cache::singleton().erase(identifier.getKey());
151
 
    }
152
 
  }
153
 
}
154
 
 
155
 
 
156
 
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
157
 
{
158
 
  /*
159
 
    We found an existing table definition. Return it if we didn't get
160
 
    an error when reading the table definition from file.
161
 
  */
162
 
 
163
 
  /* We must do a lock to ensure that the structure is initialized */
164
 
  if (share->error)
165
 
  {
166
 
    /* Table definition contained an error */
167
 
    share->open_table_error(share->error, share->open_errno, share->errarg);
168
 
 
169
 
    return TableShare::shared_ptr();
170
 
  }
171
 
 
172
 
  share->incrementTableCount();
173
 
 
174
 
  return share;
175
 
}
176
 
 
177
 
/*
178
 
  Get TableShare for a table.
179
 
 
180
 
  get_table_share()
181
 
  session                       Thread handle
182
 
  table_list            Table that should be opened
183
 
  key                   Table cache key
184
 
  key_length            Length of key
185
 
  error                 out: Error code from open_table_def()
186
 
 
187
 
  IMPLEMENTATION
188
 
  Get a table definition from the table definition cache.
189
 
  If it doesn't exist, create a new from the table definition file.
190
 
 
191
 
  NOTES
192
 
  We must have wrlock on table::Cache::singleton().mutex() when we come here
193
 
  (To be changed later)
194
 
 
195
 
  RETURN
196
 
  0  Error
197
 
#  Share for table
198
 
*/
199
 
 
200
 
TableShare::shared_ptr TableShare::getShareCreate(Session *session, 
201
 
                                                  const TableIdentifier &identifier,
202
 
                                                  int &in_error)
203
 
{
204
 
  TableShare::shared_ptr share;
205
 
 
206
 
  in_error= 0;
207
 
 
208
 
  /* Read table definition from cache */
209
 
  if ((share= definition::Cache::singleton().find(identifier.getKey())))
210
 
    return foundTableShare(share);
211
 
 
212
 
  share.reset(new TableShare(message::Table::STANDARD, identifier));
213
 
  
214
 
  if (share->open_table_def(*session, identifier))
215
 
  {
216
 
    in_error= share->error;
217
 
 
218
 
    return TableShare::shared_ptr();
219
 
  }
220
 
  share->ref_count++;                           // Mark in use
221
 
  
222
 
  plugin::EventObserver::registerTableEvents(*share);
223
 
 
224
 
  bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
225
 
 
226
 
  if (not ret)
227
 
    return TableShare::shared_ptr();
228
 
 
229
 
  return share;
230
 
}
231
 
 
232
 
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
233
 
{
234
 
  enum_field_types field_type;
235
 
 
236
 
  switch(proto_field_type)
 
98
 
 
99
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
 
100
{
 
101
  switch(field.type())
237
102
  {
238
103
  case message::Table::Field::INTEGER:
239
 
    field_type= DRIZZLE_TYPE_LONG;
240
 
    break;
 
104
    return DRIZZLE_TYPE_LONG;
 
105
 
241
106
  case message::Table::Field::DOUBLE:
242
 
    field_type= DRIZZLE_TYPE_DOUBLE;
243
 
    break;
244
 
  case message::Table::Field::TIMESTAMP:
245
 
    field_type= DRIZZLE_TYPE_TIMESTAMP;
246
 
    break;
 
107
    return DRIZZLE_TYPE_DOUBLE;
 
108
 
 
109
  case message::Table::Field::EPOCH:
 
110
    if (field.has_time_options() and field.time_options().microseconds())
 
111
      return DRIZZLE_TYPE_MICROTIME;
 
112
 
 
113
    return DRIZZLE_TYPE_TIMESTAMP;
 
114
 
247
115
  case message::Table::Field::BIGINT:
248
 
    field_type= DRIZZLE_TYPE_LONGLONG;
249
 
    break;
 
116
    return DRIZZLE_TYPE_LONGLONG;
 
117
 
250
118
  case message::Table::Field::DATETIME:
251
 
    field_type= DRIZZLE_TYPE_DATETIME;
252
 
    break;
 
119
    return DRIZZLE_TYPE_DATETIME;
 
120
 
253
121
  case message::Table::Field::DATE:
254
 
    field_type= DRIZZLE_TYPE_DATE;
255
 
    break;
 
122
    return DRIZZLE_TYPE_DATE;
 
123
 
256
124
  case message::Table::Field::VARCHAR:
257
 
    field_type= DRIZZLE_TYPE_VARCHAR;
258
 
    break;
 
125
    return DRIZZLE_TYPE_VARCHAR;
 
126
 
259
127
  case message::Table::Field::DECIMAL:
260
 
    field_type= DRIZZLE_TYPE_DECIMAL;
261
 
    break;
 
128
    return DRIZZLE_TYPE_DECIMAL;
 
129
 
262
130
  case message::Table::Field::ENUM:
263
 
    field_type= DRIZZLE_TYPE_ENUM;
264
 
    break;
 
131
    return DRIZZLE_TYPE_ENUM;
 
132
 
265
133
  case message::Table::Field::BLOB:
266
 
    field_type= DRIZZLE_TYPE_BLOB;
267
 
    break;
 
134
    return DRIZZLE_TYPE_BLOB;
 
135
 
268
136
  case message::Table::Field::UUID:
269
 
    field_type= DRIZZLE_TYPE_UUID;
270
 
    break;
271
 
  default:
272
 
    assert(0);
273
 
    abort(); // Programming error
 
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;
274
144
  }
275
145
 
276
 
  return field_type;
 
146
  abort();
277
147
}
278
148
 
279
149
static Item *default_value_item(enum_field_types field_type,
308
178
    abort();
309
179
  case DRIZZLE_TYPE_TIMESTAMP:
310
180
  case DRIZZLE_TYPE_DATETIME:
 
181
  case DRIZZLE_TYPE_TIME:
311
182
  case DRIZZLE_TYPE_DATE:
312
183
  case DRIZZLE_TYPE_ENUM:
313
184
  case DRIZZLE_TYPE_UUID:
 
185
  case DRIZZLE_TYPE_MICROTIME:
 
186
  case DRIZZLE_TYPE_BOOLEAN:
314
187
    default_item= new Item_string(default_value->c_str(),
315
188
                                  default_value->length(),
316
189
                                  system_charset_info);
349
222
 */
350
223
bool TableShare::fieldInPrimaryKey(Field *in_field) const
351
224
{
352
 
  assert(table_proto != NULL);
 
225
  assert(getTableMessage());
353
226
 
354
 
  size_t num_indexes= table_proto->indexes_size();
 
227
  size_t num_indexes= getTableMessage()->indexes_size();
355
228
 
356
229
  for (size_t x= 0; x < num_indexes; ++x)
357
230
  {
358
 
    const message::Table::Index &index= table_proto->indexes(x);
 
231
    const message::Table::Index &index= getTableMessage()->indexes(x);
359
232
    if (index.is_primary())
360
233
    {
361
234
      size_t num_parts= index.index_part_size();
369
242
  return false;
370
243
}
371
244
 
372
 
TableShare::TableShare(const TableIdentifier::Type type_arg) :
 
245
TableShare::TableShare(const identifier::Table::Type type_arg) :
373
246
  table_category(TABLE_UNKNOWN_CATEGORY),
374
247
  found_next_number_field(NULL),
375
248
  timestamp_field(NULL),
376
249
  key_info(NULL),
377
250
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
378
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),
379
256
  block_size(0),
380
257
  version(0),
381
258
  timestamp_offset(0),
382
259
  reclength(0),
383
260
  stored_rec_length(0),
384
261
  max_rows(0),
385
 
  table_proto(NULL),
 
262
  _table_message(NULL),
386
263
  storage_engine(NULL),
387
264
  tmp_table(type_arg),
388
 
  ref_count(0),
 
265
  _ref_count(0),
389
266
  null_bytes(0),
390
267
  last_null_bit_pos(0),
391
 
  fields(0),
 
268
  _field_size(0),
392
269
  rec_buff_length(0),
393
270
  keys(0),
394
271
  key_parts(0),
410
287
  error(0),
411
288
  open_errno(0),
412
289
  errarg(0),
413
 
  blob_ptr_size(0),
 
290
  blob_ptr_size(portable_sizeof_char_ptr),
414
291
  db_low_byte_first(false),
415
292
  keys_in_use(0),
416
 
  keys_for_keyread(0),
417
 
  event_observers(NULL)
 
293
  keys_for_keyread(0)
418
294
{
419
 
 
420
 
  table_charset= 0;
421
 
  memset(&db, 0, sizeof(LEX_STRING));
422
 
  memset(&table_name, 0, sizeof(LEX_STRING));
423
 
  memset(&path, 0, sizeof(LEX_STRING));
424
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
425
 
 
426
295
  if (type_arg == message::Table::INTERNAL)
427
296
  {
428
 
    TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
 
297
    identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
298
    init(private_key_for_cache.vector(), private_key_for_cache.vector());
430
299
  }
431
300
  else
434
303
  }
435
304
}
436
305
 
437
 
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
 
306
TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
438
307
  table_category(TABLE_UNKNOWN_CATEGORY),
439
308
  found_next_number_field(NULL),
440
309
  timestamp_field(NULL),
441
310
  key_info(NULL),
442
311
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
312
  table_charset(0),
443
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),
444
318
  block_size(0),
445
319
  version(0),
446
320
  timestamp_offset(0),
447
321
  reclength(0),
448
322
  stored_rec_length(0),
449
323
  max_rows(0),
450
 
  table_proto(NULL),
 
324
  _table_message(NULL),
451
325
  storage_engine(NULL),
452
326
  tmp_table(message::Table::INTERNAL),
453
 
  ref_count(0),
 
327
  _ref_count(0),
454
328
  null_bytes(0),
455
329
  last_null_bit_pos(0),
456
 
  fields(0),
 
330
  _field_size(0),
457
331
  rec_buff_length(0),
458
332
  keys(0),
459
333
  key_parts(0),
475
349
  error(0),
476
350
  open_errno(0),
477
351
  errarg(0),
478
 
  blob_ptr_size(0),
 
352
  blob_ptr_size(portable_sizeof_char_ptr),
479
353
  db_low_byte_first(false),
480
354
  keys_in_use(0),
481
 
  keys_for_keyread(0),
482
 
  event_observers(NULL)
 
355
  keys_for_keyread(0)
483
356
{
484
357
  assert(identifier.getKey() == key);
485
358
 
486
 
  table_charset= 0;
487
 
  memset(&path, 0, sizeof(LEX_STRING));
488
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
489
 
 
490
359
  private_key_for_cache= key;
491
360
 
492
361
  table_category=         TABLE_CATEGORY_TEMPORARY;
509
378
}
510
379
 
511
380
 
512
 
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
 
381
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
513
382
  table_category(TABLE_UNKNOWN_CATEGORY),
514
383
  found_next_number_field(NULL),
515
384
  timestamp_field(NULL),
516
385
  key_info(NULL),
517
386
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
387
  table_charset(0),
518
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),
519
393
  block_size(0),
520
394
  version(0),
521
395
  timestamp_offset(0),
522
396
  reclength(0),
523
397
  stored_rec_length(0),
524
398
  max_rows(0),
525
 
  table_proto(NULL),
 
399
  _table_message(NULL),
526
400
  storage_engine(NULL),
527
401
  tmp_table(identifier.getType()),
528
 
  ref_count(0),
 
402
  _ref_count(0),
529
403
  null_bytes(0),
530
404
  last_null_bit_pos(0),
531
 
  fields(0),
 
405
  _field_size(0),
532
406
  rec_buff_length(0),
533
407
  keys(0),
534
408
  key_parts(0),
550
424
  error(0),
551
425
  open_errno(0),
552
426
  errarg(0),
553
 
  blob_ptr_size(0),
 
427
  blob_ptr_size(portable_sizeof_char_ptr),
554
428
  db_low_byte_first(false),
555
429
  keys_in_use(0),
556
 
  keys_for_keyread(0),
557
 
  event_observers(NULL)
 
430
  keys_for_keyread(0)
558
431
{
559
 
  table_charset= 0;
560
 
  memset(&db, 0, sizeof(LEX_STRING));
561
 
  memset(&table_name, 0, sizeof(LEX_STRING));
562
 
  memset(&path, 0, sizeof(LEX_STRING));
563
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
564
 
 
565
432
  private_key_for_cache= identifier.getKey();
566
433
  assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
567
434
  private_normalized_path.resize(identifier.getPath().size() + 1);
584
451
/*
585
452
  Used for shares that will go into the cache.
586
453
*/
587
 
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
 
                       const TableIdentifier &identifier,
 
454
TableShare::TableShare(const identifier::Table::Type type_arg,
 
455
                       const identifier::Table &identifier,
589
456
                       char *path_arg,
590
457
                       uint32_t path_length_arg) :
591
458
  table_category(TABLE_UNKNOWN_CATEGORY),
593
460
  timestamp_field(NULL),
594
461
  key_info(NULL),
595
462
  mem_root(TABLE_ALLOC_BLOCK_SIZE),
 
463
  table_charset(0),
596
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),
597
469
  block_size(0),
598
470
  version(0),
599
471
  timestamp_offset(0),
600
472
  reclength(0),
601
473
  stored_rec_length(0),
602
474
  max_rows(0),
603
 
  table_proto(NULL),
 
475
  _table_message(NULL),
604
476
  storage_engine(NULL),
605
477
  tmp_table(type_arg),
606
 
  ref_count(0),
 
478
  _ref_count(0),
607
479
  null_bytes(0),
608
480
  last_null_bit_pos(0),
609
 
  fields(0),
 
481
  _field_size(0),
610
482
  rec_buff_length(0),
611
483
  keys(0),
612
484
  key_parts(0),
628
500
  error(0),
629
501
  open_errno(0),
630
502
  errarg(0),
631
 
  blob_ptr_size(0),
 
503
  blob_ptr_size(portable_sizeof_char_ptr),
632
504
  db_low_byte_first(false),
633
505
  keys_in_use(0),
634
 
  keys_for_keyread(0),
635
 
  event_observers(NULL)
 
506
  keys_for_keyread(0)
636
507
{
637
 
  table_charset= 0;
638
 
  memset(&db, 0, sizeof(LEX_STRING));
639
 
  memset(&table_name, 0, sizeof(LEX_STRING));
640
 
  memset(&path, 0, sizeof(LEX_STRING));
641
 
  memset(&normalized_path, 0, sizeof(LEX_STRING));
642
 
 
643
508
  char *path_buff;
644
509
  std::string _path;
645
510
 
659
524
  }
660
525
  else
661
526
  {
662
 
    TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
 
527
    identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
663
528
  }
664
529
 
665
530
  if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
694
559
 
695
560
TableShare::~TableShare() 
696
561
{
697
 
  assert(ref_count == 0);
698
 
 
699
562
  storage_engine= NULL;
700
563
 
701
 
  delete table_proto;
702
 
  table_proto= NULL;
703
 
 
704
 
  plugin::EventObserver::deregisterTableEvents(*this);
705
 
 
706
564
  mem_root.free_root(MYF(0));                 // Free's share
707
565
}
708
566
 
709
 
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
 
567
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
710
568
{
711
569
  private_key_for_cache= identifier_arg.getKey();
712
570
 
719
577
  table_name.str=    db.str + db.length + 1;
720
578
  table_name.length= strlen(table_name.str);
721
579
 
722
 
  table_proto->set_name(identifier_arg.getTableName());
723
 
  table_proto->set_schema(identifier_arg.getSchemaName());
 
580
  getTableMessage()->set_name(identifier_arg.getTableName());
 
581
  getTableMessage()->set_schema(identifier_arg.getSchemaName());
724
582
}
725
583
 
726
 
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
 
584
bool TableShare::parse_table_proto(Session& session, message::Table &table)
727
585
{
728
 
  int local_error= 0;
 
586
  drizzled::error_t local_error= EE_OK;
729
587
 
730
588
  if (! table.IsInitialized())
731
589
  {
732
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
 
590
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
591
             table.name().empty() ? " " :  table.name().c_str(),
 
592
             table.InitializationErrorString().c_str());
 
593
 
733
594
    return ER_CORRUPT_TABLE_DEFINITION;
734
595
  }
735
596
 
736
 
  setTableProto(new(nothrow) message::Table(table));
 
597
  setTableMessage(table);
737
598
 
738
599
  storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
739
600
  assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
759
620
 
760
621
  table_charset= get_charset(table_options.collation_id());
761
622
 
762
 
  if (! table_charset)
 
623
  if (not table_charset)
763
624
  {
764
 
    char errmsg[100];
765
 
    snprintf(errmsg, sizeof(errmsg),
766
 
             _("Table %s has invalid/unknown collation: %d,%s"),
767
 
             getPath(),
768
 
             table_options.collation_id(),
769
 
             table_options.collation().c_str());
770
 
    errmsg[99]='\0';
 
625
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
 
626
             table_options.collation().c_str(),
 
627
             table.name().c_str());
771
628
 
772
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
773
 
    return ER_CORRUPT_TABLE_DEFINITION;
 
629
    return ER_CORRUPT_TABLE_DEFINITION; // Historical
774
630
  }
775
631
 
776
632
  db_record_offset= 1;
777
633
 
778
 
  blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
779
 
 
780
634
  keys= table.indexes_size();
781
635
 
782
636
  key_parts= 0;
926
780
  keys_for_keyread.reset();
927
781
  set_prefix(keys_in_use, keys);
928
782
 
929
 
  fields= table.field_size();
 
783
  _field_size= table.field_size();
930
784
 
931
 
  setFields(fields + 1);
932
 
  field[fields]= NULL;
 
785
  setFields(_field_size + 1);
 
786
  _fields[_field_size]= NULL;
933
787
 
934
788
  uint32_t local_null_fields= 0;
935
789
  reclength= 0;
937
791
  std::vector<uint32_t> field_offsets;
938
792
  std::vector<uint32_t> field_pack_length;
939
793
 
940
 
  field_offsets.resize(fields);
941
 
  field_pack_length.resize(fields);
 
794
  field_offsets.resize(_field_size);
 
795
  field_pack_length.resize(_field_size);
942
796
 
943
797
  uint32_t interval_count= 0;
944
798
  uint32_t interval_parts= 0;
945
799
 
946
800
  uint32_t stored_columns_reclength= 0;
947
801
 
948
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
802
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
949
803
  {
950
804
    message::Table::Field pfield= table.field(fieldnr);
951
 
    if (pfield.constraints().is_nullable())
952
 
      local_null_fields++;
 
805
    if (pfield.constraints().is_nullable()) // Historical reference
 
806
    {
 
807
      local_null_fields++;
 
808
    }
 
809
    else if (not pfield.constraints().is_notnull())
 
810
    {
 
811
      local_null_fields++;
 
812
    }
953
813
 
954
 
    enum_field_types drizzle_field_type=
955
 
      proto_field_type_to_drizzle_type(pfield.type());
 
814
    enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
956
815
 
957
816
    field_offsets[fieldnr]= stored_columns_reclength;
958
817
 
991
850
      {
992
851
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
993
852
 
994
 
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
 
853
        field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
995
854
      }
996
855
      break;
997
856
    default:
1041
900
 
1042
901
  uint32_t interval_nr= 0;
1043
902
 
1044
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
903
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1045
904
  {
1046
905
    message::Table::Field pfield= table.field(fieldnr);
1047
906
 
1053
912
 
1054
913
    if (field_options.field_value_size() > Field_enum::max_supported_elements)
1055
914
    {
1056
 
      char errmsg[100];
1057
 
      snprintf(errmsg, sizeof(errmsg),
1058
 
               _("ENUM column %s has greater than %d possible values"),
1059
 
               pfield.name().c_str(),
1060
 
               Field_enum::max_supported_elements);
1061
 
      errmsg[99]='\0';
 
915
      my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
1062
916
 
1063
 
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1064
 
      return ER_CORRUPT_TABLE_DEFINITION;
 
917
      return ER_CORRUPT_TABLE_DEFINITION_ENUM; // Historical
1065
918
    }
1066
919
 
1067
920
 
1104
957
  /* and read the fields */
1105
958
  interval_nr= 0;
1106
959
 
1107
 
  bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
 
960
  bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
1108
961
 
1109
962
  unsigned char* null_pos= getDefaultValues();
1110
963
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1111
964
 
1112
 
  for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
 
965
  for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1113
966
  {
1114
967
    message::Table::Field pfield= table.field(fieldnr);
1115
968
 
1164
1017
 
1165
1018
    enum_field_types field_type;
1166
1019
 
1167
 
    field_type= proto_field_type_to_drizzle_type(pfield.type());
 
1020
    field_type= proto_field_type_to_drizzle_type(pfield);
1168
1021
 
1169
1022
    const CHARSET_INFO *charset= &my_charset_bin;
1170
1023
 
1209
1062
      {
1210
1063
        if (fo.scale() > DECIMAL_MAX_SCALE)
1211
1064
        {
1212
 
          local_error= 4;
 
1065
          local_error= ER_NOT_FORM_FILE;
1213
1066
 
1214
 
          return local_error;
 
1067
          return true;
1215
1068
        }
1216
1069
        decimals= static_cast<uint8_t>(fo.scale());
1217
1070
      }
1231
1084
    }
1232
1085
 
1233
1086
 
1234
 
    blob_ptr_size= portable_sizeof_char_ptr;
1235
 
 
1236
1087
    uint32_t field_length= 0; //Assignment is for compiler complaint.
1237
1088
 
 
1089
    // We set field_length in this loop.
1238
1090
    switch (field_type)
1239
1091
    {
1240
1092
    case DRIZZLE_TYPE_BLOB:
1266
1118
            decimals != NOT_FIXED_DEC)
1267
1119
        {
1268
1120
          my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1269
 
          local_error= 1;
1270
 
 
1271
 
          return local_error;
 
1121
          local_error= ER_M_BIGGER_THAN_D;
 
1122
          return true;
1272
1123
        }
1273
1124
        break;
1274
1125
      }
1276
1127
      {
1277
1128
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1278
1129
 
1279
 
        field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
1130
        field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1280
1131
                                                     false);
1281
1132
        break;
1282
1133
      }
1283
 
    case DRIZZLE_TYPE_TIMESTAMP:
1284
1134
    case DRIZZLE_TYPE_DATETIME:
1285
1135
      field_length= DateTime::MAX_STRING_LENGTH;
1286
1136
      break;
1313
1163
      }
1314
1164
      break;
1315
1165
    case DRIZZLE_TYPE_LONGLONG:
1316
 
      field_length= MAX_BIGINT_WIDTH;
 
1166
      {
 
1167
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
 
1168
        field_length= MAX_BIGINT_WIDTH+sign_len;
 
1169
      }
1317
1170
      break;
1318
1171
    case DRIZZLE_TYPE_UUID:
1319
1172
      field_length= field::Uuid::max_string_length();
1320
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();
 
1185
      break;
1321
1186
    case DRIZZLE_TYPE_NULL:
1322
1187
      abort(); // Programming error
1323
1188
    }
1324
1189
 
1325
 
    assert(enum_field_types_size == 12);
1326
 
 
1327
 
    Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
 
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,
1328
1203
                         field_length,
1329
 
                         pfield.constraints().is_nullable(),
 
1204
                         not is_not_null,
1330
1205
                         null_pos,
1331
1206
                         null_bit_pos,
1332
1207
                         decimals,
1334
1209
                         charset,
1335
1210
                         MTYP_TYPENR(unireg_type),
1336
1211
                         ((field_type == DRIZZLE_TYPE_ENUM) ?  &intervals[interval_nr++] : (TYPELIB*) 0),
1337
 
                         getTableProto()->field(fieldnr).name().c_str());
 
1212
                         getTableMessage()->field(fieldnr).name().c_str());
1338
1213
 
1339
 
    field[fieldnr]= f;
 
1214
    _fields[fieldnr]= f;
1340
1215
 
1341
1216
    // Insert post make_field code here.
1342
1217
    switch (field_type)
1346
1221
    case DRIZZLE_TYPE_DOUBLE:
1347
1222
    case DRIZZLE_TYPE_DECIMAL:
1348
1223
    case DRIZZLE_TYPE_TIMESTAMP:
 
1224
    case DRIZZLE_TYPE_TIME:
1349
1225
    case DRIZZLE_TYPE_DATETIME:
 
1226
    case DRIZZLE_TYPE_MICROTIME:
1350
1227
    case DRIZZLE_TYPE_DATE:
1351
1228
    case DRIZZLE_TYPE_ENUM:
1352
1229
    case DRIZZLE_TYPE_LONG:
1353
1230
    case DRIZZLE_TYPE_LONGLONG:
1354
1231
    case DRIZZLE_TYPE_NULL:
1355
1232
    case DRIZZLE_TYPE_UUID:
 
1233
    case DRIZZLE_TYPE_BOOLEAN:
1356
1234
      break;
1357
1235
    }
1358
1236
 
1380
1258
      if (res != 0 && res != 3) /* @TODO Huh? */
1381
1259
      {
1382
1260
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1383
 
        local_error= 1;
 
1261
        local_error= ER_INVALID_DEFAULT;
1384
1262
 
1385
 
        return local_error;
 
1263
        return true;
1386
1264
      }
1387
1265
    }
1388
1266
    else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1401
1279
 
1402
1280
    f->setPosition(fieldnr);
1403
1281
    f->comment= comment;
1404
 
    if (! default_value &&
1405
 
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
 
1282
    if (not default_value &&
 
1283
        not (f->unireg_check==Field::NEXT_NUMBER) &&
1406
1284
        (f->flags & NOT_NULL_FLAG) &&
1407
 
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
 
1285
        (not f->is_timestamp()))
1408
1286
    {
1409
1287
      f->flags|= NO_DEFAULT_VALUE_FLAG;
1410
1288
    }
1411
1289
 
1412
1290
    if (f->unireg_check == Field::NEXT_NUMBER)
1413
 
      found_next_number_field= &(field[fieldnr]);
 
1291
      found_next_number_field= &(_fields[fieldnr]);
1414
1292
 
1415
1293
    if (use_hash) /* supposedly this never fails... but comments lie */
1416
1294
    {
1417
 
      const char *local_field_name= field[fieldnr]->field_name;
1418
 
      name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
 
1295
      const char *local_field_name= _fields[fieldnr]->field_name;
 
1296
      name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1419
1297
    }
1420
1298
  }
1421
1299
 
1471
1349
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1472
1350
        {
1473
1351
          uint32_t fieldnr= key_part[i].fieldnr;
1474
 
          if (! fieldnr ||
1475
 
              field[fieldnr-1]->null_ptr ||
1476
 
              field[fieldnr-1]->key_length() != key_part[i].length)
 
1352
          if (not fieldnr ||
 
1353
              _fields[fieldnr-1]->null_ptr ||
 
1354
              _fields[fieldnr-1]->key_length() != key_part[i].length)
1477
1355
          {
1478
1356
            local_primary_key= MAX_KEY; // Can't be used
1479
1357
            break;
1488
1366
        {
1489
1367
          return ENOMEM;
1490
1368
        }
1491
 
        local_field= key_part->field= field[key_part->fieldnr-1];
 
1369
        local_field= key_part->field= _fields[key_part->fieldnr-1];
1492
1370
        key_part->type= local_field->key_type();
1493
1371
        if (local_field->null_ptr)
1494
1372
        {
1594
1472
                            &next_number_keypart)) < 0)
1595
1473
    {
1596
1474
      /* Wrong field definition */
1597
 
      local_error= 4;
 
1475
      local_error= ER_NOT_FORM_FILE;
1598
1476
 
1599
 
      return local_error;
 
1477
      return true;
1600
1478
    }
1601
1479
    else
1602
1480
    {
1610
1488
    blob_field.resize(blob_fields);
1611
1489
    uint32_t *save= &blob_field[0];
1612
1490
    uint32_t k= 0;
1613
 
    for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
 
1491
    for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1614
1492
    {
1615
1493
      if ((*iter)->flags & BLOB_FLAG)
1616
1494
        (*save++)= k;
1618
1496
  }
1619
1497
 
1620
1498
  all_set.clear();
1621
 
  all_set.resize(fields);
 
1499
  all_set.resize(_field_size);
1622
1500
  all_set.set();
1623
1501
 
1624
 
  return local_error;
1625
 
}
1626
 
 
1627
 
int TableShare::parse_table_proto(Session& session, message::Table &table)
1628
 
{
1629
 
  int local_error= inner_parse_table_proto(session, table);
1630
 
 
1631
 
  if (not local_error)
1632
 
    return 0;
1633
 
 
1634
 
  error= local_error;
1635
 
  open_errno= errno;
1636
 
  errarg= 0;
1637
 
  open_table_error(local_error, open_errno, 0);
1638
 
 
1639
 
  return local_error;
1640
 
}
1641
 
 
 
1502
  return local_error != EE_OK;
 
1503
}
1642
1504
 
1643
1505
/*
1644
1506
  Read table definition from a binary / text based .frm cursor
1664
1526
  6    Unknown .frm version
1665
1527
*/
1666
1528
 
1667
 
int TableShare::open_table_def(Session& session, const TableIdentifier &identifier)
 
1529
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1668
1530
{
1669
 
  int local_error;
1670
 
  bool error_given;
1671
 
 
1672
 
  local_error= 1;
1673
 
  error_given= 0;
1674
 
 
1675
 
  message::table::shared_ptr table;
1676
 
 
1677
 
  local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1678
 
 
1679
 
  if (local_error != EEXIST)
 
1531
  drizzled::error_t local_error= EE_OK;
 
1532
 
 
1533
  message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
 
1534
 
 
1535
  if (table and table->IsInitialized())
1680
1536
  {
1681
 
    if (local_error > 0)
 
1537
    if (parse_table_proto(session, *table))
1682
1538
    {
1683
 
      errno= local_error;
1684
 
      local_error= 1;
 
1539
      local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1540
      my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1685
1541
    }
1686
1542
    else
1687
1543
    {
1688
 
      if (not table->IsInitialized())
1689
 
      {
1690
 
        local_error= 4;
1691
 
      }
 
1544
      setTableCategory(TABLE_CATEGORY_USER);
 
1545
      local_error= EE_OK;
1692
1546
    }
1693
 
    goto err_not_open;
1694
 
  }
1695
 
 
1696
 
  local_error= parse_table_proto(session, *table);
1697
 
 
1698
 
  setTableCategory(TABLE_CATEGORY_USER);
1699
 
 
1700
 
err_not_open:
1701
 
  if (local_error && !error_given)
1702
 
  {
1703
 
    error= local_error;
1704
 
    open_table_error(error, (open_errno= errno), 0);
1705
 
  }
1706
 
 
1707
 
  return(error);
 
1547
  }
 
1548
  else if (table and not table->IsInitialized())
 
1549
  {
 
1550
    local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
 
1551
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
1552
  }
 
1553
  else
 
1554
  {
 
1555
    local_error= ER_TABLE_UNKNOWN;
 
1556
    my_error(ER_TABLE_UNKNOWN, identifier);
 
1557
  }
 
1558
 
 
1559
  return static_cast<int>(local_error);
1708
1560
}
1709
1561
 
1710
1562
 
1732
1584
  7    Table definition has changed in engine
1733
1585
*/
1734
1586
int TableShare::open_table_from_share(Session *session,
1735
 
                                      const TableIdentifier &identifier,
 
1587
                                      const identifier::Table &identifier,
1736
1588
                                      const char *alias,
1737
1589
                                      uint32_t db_stat, uint32_t ha_open_flags,
1738
1590
                                      Table &outparam)
1749
1601
  if (not error_reported)
1750
1602
    open_table_error(ret, errno, 0);
1751
1603
 
1752
 
  delete outparam.cursor;
 
1604
  boost::checked_delete(outparam.cursor);
1753
1605
  outparam.cursor= 0;                           // For easier error checking
1754
1606
  outparam.db_stat= 0;
1755
1607
  outparam.getMemRoot()->free_root(MYF(0));       // Safe to call on zeroed root
1768
1620
  unsigned char *record= NULL;
1769
1621
  Field **field_ptr;
1770
1622
 
1771
 
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1772
 
  assert(session->lex->is_lex_started);
1773
 
 
1774
1623
  local_error= 1;
1775
1624
  outparam.resetTable(session, this, db_stat);
1776
1625
 
1822
1671
    memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1823
1672
  }
1824
1673
 
1825
 
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
 
1674
  if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((_field_size+1)* sizeof(Field*)))))
1826
1675
  {
1827
1676
    return local_error;
1828
1677
  }
1834
1683
  outparam.null_flags= (unsigned char*) record+1;
1835
1684
 
1836
1685
  /* Setup copy of fields from share, but use the right alias and record */
1837
 
  for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
 
1686
  for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1838
1687
  {
1839
 
    if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
 
1688
    if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1840
1689
      return local_error;
1841
1690
  }
1842
1691
  (*field_ptr)= 0;                              // End marker
1845
1694
    outparam.found_next_number_field=
1846
1695
      outparam.getField(positionFields(found_next_number_field));
1847
1696
  if (timestamp_field)
1848
 
    outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
 
1697
    outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1849
1698
 
1850
1699
  /* Fix key->name and key_part->field */
1851
1700
  if (key_parts)
1894
1743
 
1895
1744
  /* Allocate bitmaps */
1896
1745
 
1897
 
  outparam.def_read_set.resize(fields);
1898
 
  outparam.def_write_set.resize(fields);
1899
 
  outparam.tmp_set.resize(fields);
 
1746
  outparam.def_read_set.resize(_field_size);
 
1747
  outparam.def_write_set.resize(_field_size);
 
1748
  outparam.tmp_set.resize(_field_size);
1900
1749
  outparam.default_column_bitmaps();
1901
1750
 
1902
1751
  return 0;
1903
1752
}
1904
1753
 
1905
 
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
 
1754
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1906
1755
                                        uint32_t db_stat, uint32_t ha_open_flags,
1907
1756
                                        Table &outparam,
1908
1757
                                        bool &error_reported)
1953
1802
/* error message when opening a form cursor */
1954
1803
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1955
1804
{
1956
 
  int err_no;
1957
1805
  char buff[FN_REFLEN];
1958
1806
  myf errortype= ME_ERROR+ME_WAITTANG;
1959
1807
 
1962
1810
  case 1:
1963
1811
    if (db_errno == ENOENT)
1964
1812
    {
1965
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
 
1813
      identifier::Table identifier(db.str, table_name.str);
 
1814
      my_error(ER_TABLE_UNKNOWN, identifier);
1966
1815
    }
1967
1816
    else
1968
1817
    {
1973
1822
    break;
1974
1823
  case 2:
1975
1824
    {
 
1825
      drizzled::error_t err_no;
 
1826
 
1976
1827
      err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1977
1828
        ER_FILE_USED : ER_CANT_OPEN_FILE;
 
1829
 
1978
1830
      my_error(err_no, errortype, normalized_path.str, db_errno);
1979
1831
      break;
1980
1832
    }
2010
1862
  return;
2011
1863
} /* open_table_error */
2012
1864
 
2013
 
Field *TableShare::make_field(unsigned char *ptr,
 
1865
Field *TableShare::make_field(const message::Table::Field &pfield,
 
1866
                              unsigned char *ptr,
2014
1867
                              uint32_t field_length,
2015
1868
                              bool is_nullable,
2016
1869
                              unsigned char *null_pos,
2022
1875
                              TYPELIB *interval,
2023
1876
                              const char *field_name)
2024
1877
{
 
1878
  return make_field(pfield,
 
1879
                    ptr,
 
1880
                    field_length,
 
1881
                    is_nullable,
 
1882
                    null_pos,
 
1883
                    null_bit,
 
1884
                    decimals,
 
1885
                    field_type,
 
1886
                    field_charset,
 
1887
                    unireg_check,
 
1888
                    interval,
 
1889
                    field_name,
 
1890
                    pfield.constraints().is_unsigned());
 
1891
}
 
1892
 
 
1893
Field *TableShare::make_field(const message::Table::Field &,
 
1894
                              unsigned char *ptr,
 
1895
                              uint32_t field_length,
 
1896
                              bool is_nullable,
 
1897
                              unsigned char *null_pos,
 
1898
                              unsigned char null_bit,
 
1899
                              uint8_t decimals,
 
1900
                              enum_field_types field_type,
 
1901
                              const CHARSET_INFO * field_charset,
 
1902
                              Field::utype unireg_check,
 
1903
                              TYPELIB *interval,
 
1904
                              const char *field_name, 
 
1905
                              bool is_unsigned)
 
1906
{
2025
1907
  if (! is_nullable)
2026
1908
  {
2027
1909
    null_pos=0;
2032
1914
    null_bit= ((unsigned char) 1) << null_bit;
2033
1915
  }
2034
1916
 
2035
 
  switch (field_type) 
2036
 
  {
2037
 
  case DRIZZLE_TYPE_DATE:
2038
 
  case DRIZZLE_TYPE_DATETIME:
2039
 
  case DRIZZLE_TYPE_TIMESTAMP:
2040
 
  case DRIZZLE_TYPE_UUID:
2041
 
    field_charset= &my_charset_bin;
2042
 
  default: break;
2043
 
  }
2044
 
 
2045
1917
  switch (field_type)
2046
1918
  {
2047
1919
  case DRIZZLE_TYPE_ENUM:
2048
1920
    return new (&mem_root) Field_enum(ptr,
2049
 
                                 field_length,
2050
 
                                 null_pos,
2051
 
                                 null_bit,
2052
 
                                 field_name,
2053
 
                                 interval,
2054
 
                                 field_charset);
 
1921
                                      field_length,
 
1922
                                      null_pos,
 
1923
                                      null_bit,
 
1924
                                      field_name,
 
1925
                                      interval,
 
1926
                                      field_charset);
2055
1927
  case DRIZZLE_TYPE_VARCHAR:
2056
1928
    setVariableWidth();
2057
1929
    return new (&mem_root) Field_varstring(ptr,field_length,
2061
1933
                                      field_charset);
2062
1934
  case DRIZZLE_TYPE_BLOB:
2063
1935
    return new (&mem_root) Field_blob(ptr,
2064
 
                                 null_pos,
2065
 
                                 null_bit,
2066
 
                                 field_name,
2067
 
                                 this,
2068
 
                                 field_charset);
 
1936
                                      null_pos,
 
1937
                                      null_bit,
 
1938
                                      field_name,
 
1939
                                      this,
 
1940
                                      field_charset);
2069
1941
  case DRIZZLE_TYPE_DECIMAL:
2070
1942
    return new (&mem_root) Field_decimal(ptr,
2071
 
                                    field_length,
2072
 
                                    null_pos,
2073
 
                                    null_bit,
2074
 
                                    unireg_check,
2075
 
                                    field_name,
2076
 
                                    decimals,
2077
 
                                    false,
2078
 
                                    false /* is_unsigned */);
 
1943
                                         field_length,
 
1944
                                         null_pos,
 
1945
                                         null_bit,
 
1946
                                         unireg_check,
 
1947
                                         field_name,
 
1948
                                         decimals);
2079
1949
  case DRIZZLE_TYPE_DOUBLE:
2080
1950
    return new (&mem_root) Field_double(ptr,
2081
1951
                                   field_length,
2092
1962
                                       null_pos,
2093
1963
                                       null_bit,
2094
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);
2095
1972
  case DRIZZLE_TYPE_LONG:
2096
1973
    return new (&mem_root) field::Int32(ptr,
2097
1974
                                        field_length,
2100
1977
                                        unireg_check,
2101
1978
                                        field_name);
2102
1979
  case DRIZZLE_TYPE_LONGLONG:
2103
 
    return new (&mem_root) field::Int64(ptr,
2104
 
                                        field_length,
2105
 
                                        null_pos,
2106
 
                                        null_bit,
2107
 
                                        unireg_check,
2108
 
                                        field_name);
 
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);
2109
2005
  case DRIZZLE_TYPE_TIMESTAMP:
2110
 
    return new (&mem_root) Field_timestamp(ptr,
2111
 
                                      field_length,
2112
 
                                      null_pos,
2113
 
                                      null_bit,
2114
 
                                      unireg_check,
2115
 
                                      field_name,
2116
 
                                      this,
2117
 
                                      field_charset);
 
2006
    return new (&mem_root) field::Epoch(ptr,
 
2007
                                        null_pos,
 
2008
                                        null_bit,
 
2009
                                        unireg_check,
 
2010
                                        field_name,
 
2011
                                        this);
 
2012
  case DRIZZLE_TYPE_TIME:
 
2013
    return new (&mem_root) field::Time(ptr,
 
2014
                                       field_length,
 
2015
                                       null_pos,
 
2016
                                       null_bit,
 
2017
                                       field_name);
2118
2018
  case DRIZZLE_TYPE_DATE:
2119
2019
    return new (&mem_root) Field_date(ptr,
2120
2020
                                 null_pos,
2121
2021
                                 null_bit,
2122
 
                                 field_name,
2123
 
                                 field_charset);
 
2022
                                 field_name);
2124
2023
  case DRIZZLE_TYPE_DATETIME:
2125
2024
    return new (&mem_root) Field_datetime(ptr,
2126
2025
                                     null_pos,
2127
2026
                                     null_bit,
2128
 
                                     field_name,
2129
 
                                     field_charset);
 
2027
                                     field_name);
2130
2028
  case DRIZZLE_TYPE_NULL:
2131
2029
    return new (&mem_root) Field_null(ptr,
2132
 
                                 field_length,
2133
 
                                 field_name,
2134
 
                                 field_charset);
 
2030
                                      field_length,
 
2031
                                      field_name);
2135
2032
  }
2136
2033
  assert(0);
2137
2034
  abort();
2138
2035
}
2139
2036
 
 
2037
void TableShare::refreshVersion()
 
2038
{
 
2039
  version= refresh_version;
 
2040
}
 
2041
 
2140
2042
 
2141
2043
} /* namespace drizzled */