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>
46
#include <drizzled/table.h>
47
#include <drizzled/table/shell.h>
49
#include <drizzled/session.h>
51
#include <drizzled/charset.h>
52
#include <drizzled/internal/m_string.h>
53
#include <drizzled/internal/my_sys.h>
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>
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>
84
#include <drizzled/plugin/storage_engine.h>
86
#include <drizzled/definition/cache.h>
87
#include <drizzled/typelib.h>
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"
47
#include "drizzled/session.h"
49
#include "drizzled/charset.h"
50
#include "drizzled/internal/m_string.h"
51
#include "drizzled/internal/my_sys.h"
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"
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"
91
77
using namespace std;
96
82
extern size_t table_def_size;
99
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
83
TableDefinitionCache table_def_cache;
84
static pthread_mutex_t LOCK_table_share;
85
bool table_def_inited= false;
87
/*****************************************************************************
88
Functions to handle table definition cach (TableShare)
89
*****************************************************************************/
92
void TableShare::cacheStart(void)
94
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
95
table_def_inited= true;
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.
101
table_def_cache.rehash(table_def_size);
105
void TableShare::cacheStop(void)
107
if (table_def_inited)
109
table_def_inited= false;
110
pthread_mutex_destroy(&LOCK_table_share);
116
* @TODO: This should return size_t
118
uint32_t cached_table_definitions(void)
120
return static_cast<uint32_t>(table_def_cache.size());
125
Mark that we are not using table share anymore.
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.
136
void TableShare::release(TableShare *share)
138
bool to_be_deleted= false;
139
safe_mutex_assert_owner(&LOCK_open);
141
pthread_mutex_lock(&share->mutex);
142
if (!--share->ref_count)
149
const string key_string(share->getCacheKey(),
150
share->getCacheKeySize());
151
plugin::EventObserver::deregisterTableEvents(*share);
153
TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
154
if (iter != table_def_cache.end())
156
table_def_cache.erase(iter);
161
pthread_mutex_unlock(&share->mutex);
164
void TableShare::release(const char *key, uint32_t key_length)
166
const string key_string(key, key_length);
168
TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
169
if (iter != table_def_cache.end())
171
TableShare *share= (*iter).second;
172
share->version= 0; // Mark for delete
173
if (share->ref_count == 0)
175
pthread_mutex_lock(&share->mutex);
176
plugin::EventObserver::deregisterTableEvents(*share);
177
table_def_cache.erase(key_string);
184
static TableShare *foundTableShare(TableShare *share)
187
We found an existing table definition. Return it if we didn't get
188
an error when reading the table definition from file.
191
/* We must do a lock to ensure that the structure is initialized */
192
(void) pthread_mutex_lock(&share->mutex);
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);
202
share->incrementTableCount();
203
(void) pthread_mutex_unlock(&share->mutex);
209
Get TableShare for a table.
212
session Thread handle
213
table_list Table that should be opened
215
key_length Length of key
216
error out: Error code from open_table_def()
219
Get a table definition from the table definition cache.
220
If it doesn't exist, create a new from the table definition file.
223
We must have wrlock on LOCK_open when we come here
224
(To be changed later)
231
TableShare *TableShare::getShare(Session *session,
233
uint32_t key_length, int *error)
235
const string key_string(key, key_length);
236
TableShare *share= NULL;
240
/* Read table definition from cache */
241
TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
242
if (iter != table_def_cache.end())
244
share= (*iter).second;
245
return foundTableShare(share);
248
if (not (share= new TableShare(key, key_length)))
254
Lock mutex to be able to read table definition from file without
257
(void) pthread_mutex_lock(&share->mutex);
260
* @TODO: we need to eject something if we exceed table_def_size
262
pair<TableDefinitionCache::iterator, bool> ret=
263
table_def_cache.insert(make_pair(key_string, share));
264
if (ret.second == false)
271
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
272
if (share->open_table_def(*session, identifier))
274
*error= share->error;
275
table_def_cache.erase(key_string);
280
share->ref_count++; // Mark in use
282
plugin::EventObserver::registerTableEvents(*share);
284
(void) pthread_mutex_unlock(&share->mutex);
291
Check if table definition exits in cache
294
get_cached_table_share()
296
table_name Table name
300
# TableShare for table
302
TableShare *TableShare::getShare(TableIdentifier &identifier)
304
char key[MAX_DBKEY_LENGTH];
306
safe_mutex_assert_owner(&LOCK_open);
308
key_length= TableShare::createKey(key, identifier);
310
const string key_string(key, key_length);
312
TableDefinitionCache::iterator iter= table_def_cache.find(key_string);
313
if (iter != table_def_cache.end())
315
return (*iter).second;
323
/* Get column name from column hash */
325
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
327
*length= (uint32_t) strlen((*buff)->field_name);
328
return (unsigned char*) (*buff)->field_name;
331
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
333
enum_field_types field_type;
335
switch(proto_field_type)
103
337
case message::Table::Field::INTEGER:
104
return DRIZZLE_TYPE_LONG;
338
field_type= DRIZZLE_TYPE_LONG;
106
340
case message::Table::Field::DOUBLE:
107
return DRIZZLE_TYPE_DOUBLE;
109
case message::Table::Field::EPOCH:
110
if (field.has_time_options() and field.time_options().microseconds())
111
return DRIZZLE_TYPE_MICROTIME;
113
return DRIZZLE_TYPE_TIMESTAMP;
341
field_type= DRIZZLE_TYPE_DOUBLE;
343
case message::Table::Field::TIMESTAMP:
344
field_type= DRIZZLE_TYPE_TIMESTAMP;
115
346
case message::Table::Field::BIGINT:
116
return DRIZZLE_TYPE_LONGLONG;
347
field_type= DRIZZLE_TYPE_LONGLONG;
118
349
case message::Table::Field::DATETIME:
119
return DRIZZLE_TYPE_DATETIME;
350
field_type= DRIZZLE_TYPE_DATETIME;
121
352
case message::Table::Field::DATE:
122
return DRIZZLE_TYPE_DATE;
353
field_type= DRIZZLE_TYPE_DATE;
124
355
case message::Table::Field::VARCHAR:
125
return DRIZZLE_TYPE_VARCHAR;
356
field_type= DRIZZLE_TYPE_VARCHAR;
127
358
case message::Table::Field::DECIMAL:
128
return DRIZZLE_TYPE_DECIMAL;
359
field_type= DRIZZLE_TYPE_DECIMAL;
130
361
case message::Table::Field::ENUM:
131
return DRIZZLE_TYPE_ENUM;
362
field_type= DRIZZLE_TYPE_ENUM;
133
364
case message::Table::Field::BLOB:
134
return DRIZZLE_TYPE_BLOB;
136
case message::Table::Field::UUID:
137
return DRIZZLE_TYPE_UUID;
139
case message::Table::Field::BOOLEAN:
140
return DRIZZLE_TYPE_BOOLEAN;
142
case message::Table::Field::TIME:
143
return DRIZZLE_TYPE_TIME;
365
field_type= DRIZZLE_TYPE_BLOB;
368
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
149
375
static Item *default_value_item(enum_field_types field_type,
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),
250
mem_root(TABLE_ALLOC_BLOCK_SIZE),
253
table_name(NULL_LEX_STRING),
254
path(NULL_LEX_STRING),
255
normalized_path(NULL_LEX_STRING),
260
stored_rec_length(0),
262
_table_message(NULL),
263
storage_engine(NULL),
267
last_null_bit_pos(0),
273
max_unique_length(0),
278
has_variable_width(false),
279
db_create_options(0),
280
db_options_in_use(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),
290
blob_ptr_size(portable_sizeof_char_ptr),
291
db_low_byte_first(false),
295
if (type_arg == message::Table::INTERNAL)
297
identifier::Table::build_tmptable_filename(private_key_for_cache.vectorPtr());
298
init(private_key_for_cache.vector(), private_key_for_cache.vector());
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),
311
mem_root(TABLE_ALLOC_BLOCK_SIZE),
315
table_name(NULL_LEX_STRING),
316
path(NULL_LEX_STRING),
317
normalized_path(NULL_LEX_STRING),
322
stored_rec_length(0),
324
_table_message(NULL),
325
storage_engine(NULL),
326
tmp_table(message::Table::INTERNAL),
329
last_null_bit_pos(0),
335
max_unique_length(0),
340
has_variable_width(false),
341
db_create_options(0),
342
db_options_in_use(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),
352
blob_ptr_size(portable_sizeof_char_ptr),
353
db_low_byte_first(false),
357
assert(identifier.getKey() == key);
359
private_key_for_cache= key;
361
table_category= TABLE_CATEGORY_TEMPORARY;
362
tmp_table= message::Table::INTERNAL;
364
db.str= const_cast<char *>(private_key_for_cache.vector());
365
db.length= strlen(private_key_for_cache.vector());
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;
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);
377
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
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),
386
mem_root(TABLE_ALLOC_BLOCK_SIZE),
390
table_name(NULL_LEX_STRING),
391
path(NULL_LEX_STRING),
392
normalized_path(NULL_LEX_STRING),
397
stored_rec_length(0),
399
_table_message(NULL),
400
storage_engine(NULL),
401
tmp_table(identifier.getType()),
404
last_null_bit_pos(0),
410
max_unique_length(0),
415
has_variable_width(false),
416
db_create_options(0),
417
db_options_in_use(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),
427
blob_ptr_size(portable_sizeof_char_ptr),
428
db_low_byte_first(false),
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());
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();
452
Used for shares that will go into the cache.
454
TableShare::TableShare(const identifier::Table::Type type_arg,
455
const identifier::Table &identifier,
457
uint32_t path_length_arg) :
458
table_category(TABLE_UNKNOWN_CATEGORY),
459
found_next_number_field(NULL),
460
timestamp_field(NULL),
462
mem_root(TABLE_ALLOC_BLOCK_SIZE),
466
table_name(NULL_LEX_STRING),
467
path(NULL_LEX_STRING),
468
normalized_path(NULL_LEX_STRING),
473
stored_rec_length(0),
475
_table_message(NULL),
476
storage_engine(NULL),
480
last_null_bit_pos(0),
486
max_unique_length(0),
491
has_variable_width(false),
492
db_create_options(0),
493
db_options_in_use(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),
503
blob_ptr_size(portable_sizeof_char_ptr),
504
db_low_byte_first(false),
468
TableDefinitionCache &TableShare::getCache()
470
return table_def_cache;
473
TableShare::TableShare(char *key, uint32_t key_length, char *path_arg, uint32_t path_length_arg) :
474
table_category(TABLE_UNKNOWN_CATEGORY),
476
found_next_number_field(NULL),
477
timestamp_field(NULL),
484
stored_rec_length(0),
485
row_type(ROW_TYPE_DEFAULT),
488
storage_engine(NULL),
489
tmp_table(message::Table::STANDARD),
492
last_null_bit_pos(0),
498
max_unique_length(0),
503
timestamp_field_offset(0),
505
db_create_options(0),
506
db_options_in_use(0),
508
rowid_field_offset(0),
510
next_number_index(0),
511
next_number_key_offset(0),
512
next_number_keypart(0),
516
column_bitmap_size(0),
518
db_low_byte_first(false),
520
replace_with_name_lock(false),
521
waiting_on_cond(false),
524
event_observers(NULL),
527
memset(&name_hash, 0, sizeof(HASH));
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));
536
mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
537
char *key_buff, *path_buff;
509
538
std::string _path;
511
private_key_for_cache= identifier.getKey();
513
Let us use the fact that the key is "db/0/table_name/0" + optional
514
part for temporary tables.
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;
541
db.length= strlen(db.str);
542
table_name.str= db.str + db.length + 1;
519
543
table_name.length= strlen(table_name.str);
527
identifier::Table::build_table_filename(_path, db.str, table_name.str, false);
551
build_table_filename(_path, db.str, table_name.str, false);
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,
558
memcpy(key_buff, key, key_length);
559
set_table_cache_key(key_buff, key_length, db.length, table_name.length);
532
561
setPath(path_buff, _path.length());
533
562
strcpy(path_buff, _path.c_str());
534
563
setNormalizedPath(path_buff, _path.length());
536
565
version= refresh_version;
567
pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
568
pthread_cond_init(&cond, NULL);
540
572
assert(0); // We should throw here.
545
void TableShare::init(const char *new_table_name,
546
const char *new_path)
549
table_category= TABLE_CATEGORY_TEMPORARY;
550
tmp_table= message::Table::INTERNAL;
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);
560
TableShare::~TableShare()
562
storage_engine= NULL;
564
mem_root.free_root(MYF(0)); // Free's share
567
void TableShare::setIdentifier(const identifier::Table &identifier_arg)
569
private_key_for_cache= identifier_arg.getKey();
572
Let us use the fact that the key is "db/0/table_name/0" + optional
573
part for temporary tables.
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);
580
getTableMessage()->set_name(identifier_arg.getTableName());
581
getTableMessage()->set_schema(identifier_arg.getSchemaName());
584
bool TableShare::parse_table_proto(Session& session, message::Table &table)
586
drizzled::error_t local_error= EE_OK;
578
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
588
582
if (! table.IsInitialized())
590
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
591
table.name().empty() ? " " : table.name().c_str(),
592
table.InitializationErrorString().c_str());
584
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
585
return ER_CORRUPT_TABLE_DEFINITION;
597
setTableMessage(table);
588
setTableProto(new(nothrow) message::Table(table));
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.
780
765
keys_for_keyread.reset();
781
766
set_prefix(keys_in_use, keys);
783
_field_size= table.field_size();
768
fields= table.field_size();
785
setFields(_field_size + 1);
786
_fields[_field_size]= NULL;
770
setFields(fields + 1);
788
773
uint32_t local_null_fields= 0;
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;
794
field_offsets.resize(_field_size);
795
field_pack_length.resize(_field_size);
779
field_offsets.resize(fields);
780
field_pack_length.resize(fields);
797
782
uint32_t interval_count= 0;
798
783
uint32_t interval_parts= 0;
800
785
uint32_t stored_columns_reclength= 0;
802
for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
787
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
804
789
message::Table::Field pfield= table.field(fieldnr);
805
if (pfield.constraints().is_nullable()) // Historical reference
809
else if (not pfield.constraints().is_notnull())
790
if (pfield.constraints().is_nullable())
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());
816
796
field_offsets[fieldnr]= stored_columns_reclength;
1165
1167
case DRIZZLE_TYPE_LONGLONG:
1167
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1168
field_length= MAX_BIGINT_WIDTH+sign_len;
1171
case DRIZZLE_TYPE_UUID:
1172
field_length= field::Uuid::max_string_length();
1174
case DRIZZLE_TYPE_BOOLEAN:
1175
field_length= field::Boolean::max_string_length();
1177
case DRIZZLE_TYPE_MICROTIME:
1178
field_length= field::Microtime::max_string_length();
1180
case DRIZZLE_TYPE_TIMESTAMP:
1181
field_length= field::Epoch::max_string_length();
1183
case DRIZZLE_TYPE_TIME:
1184
field_length= field::Time::max_string_length();
1168
field_length= MAX_BIGINT_WIDTH;
1186
1170
case DRIZZLE_TYPE_NULL:
1187
1171
abort(); // Programming error
1190
bool is_not_null= false;
1192
if (not pfield.constraints().is_nullable())
1196
else if (pfield.constraints().is_notnull())
1201
Field* f= make_field(pfield,
1202
record + field_offsets[fieldnr] + data_offset,
1210
MTYP_TYPENR(unireg_type),
1211
((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1212
getTableMessage()->field(fieldnr).name().c_str());
1214
_fields[fieldnr]= f;
1216
// Insert post make_field code here.
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:
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,
1176
pfield.constraints().is_nullable(),
1182
(Field::utype) MTYP_TYPENR(unireg_type),
1183
((field_type == DRIZZLE_TYPE_ENUM) ?
1184
&intervals[interval_nr++]
1186
getTableProto()->field(fieldnr).name().c_str());
1242
1190
f->init(&temp_table); /* blob default values need table obj */
1526
1508
6 Unknown .frm version
1529
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1511
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1531
drizzled::error_t local_error= EE_OK;
1533
message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1535
if (table and table->IsInitialized())
1519
message::Table table;
1521
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1523
if (local_error != EEXIST)
1537
if (parse_table_proto(session, *table))
1525
if (local_error > 0)
1539
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1544
setTableCategory(TABLE_CATEGORY_USER);
1532
if (not table.IsInitialized())
1548
else if (table and not table->IsInitialized())
1550
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1551
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1555
local_error= ER_TABLE_UNKNOWN;
1556
my_error(ER_TABLE_UNKNOWN, identifier);
1559
return static_cast<int>(local_error);
1540
local_error= parse_table_proto(session, table);
1542
setTableCategory(TABLE_CATEGORY_USER);
1545
if (local_error && !error_given)
1548
open_table_error(error, (open_errno= errno), 0);
1583
1575
5 Error (see open_table_error: charset unavailable)
1584
1576
7 Table definition has changed in engine
1586
int TableShare::open_table_from_share(Session *session,
1587
const identifier::Table &identifier,
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)
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);
1596
ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1601
if (not error_reported)
1602
open_table_error(ret, errno, 0);
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();
1613
int TableShare::open_table_from_share_inner(Session *session,
1620
unsigned char *record= NULL;
1586
unsigned char *record, *bitmaps;
1621
1587
Field **field_ptr;
1589
/* Parsing of partitioning information from .frm needs session->lex set up. */
1590
assert(session->lex->is_lex_started);
1623
1592
local_error= 1;
1624
1593
outparam.resetTable(session, this, db_stat);
1626
outparam.setAlias(alias);
1596
if (not (outparam.alias= strdup(alias)))
1628
1599
/* Allocate Cursor */
1629
if (not (outparam.cursor= db_type()->getCursor(outparam)))
1600
if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
1632
1603
local_error= 4;
1650
1621
if (records > 1)
1651
1622
outparam.record[1]= record+ rec_buff_length;
1653
outparam.record[1]= outparam.getInsertRecord(); // Safety
1624
outparam.record[1]= outparam.record[0]; // Safety
1656
#ifdef HAVE_VALGRIND
1658
1629
We need this because when we read var-length rows, we are not updating
1659
1630
bytes after end of varchar
1661
1632
if (records > 1)
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);
1669
1640
if (records > 1)
1671
memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1642
memcpy(outparam.record[1], getDefaultValues(), null_bytes);
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*)))))
1679
1650
outparam.setFields(field_ptr);
1681
record= (unsigned char*) outparam.getInsertRecord()-1; /* Fieldstart = 1 */
1652
record= (unsigned char*) outparam.record[0]-1; /* Fieldstart = 1 */
1683
1654
outparam.null_flags= (unsigned char*) record+1;
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++)
1688
if (!((*field_ptr)= _fields[i]->clone(outparam.getMemRoot(), &outparam)))
1659
if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1691
1662
(*field_ptr)= 0; // End marker
1744
1716
/* Allocate bitmaps */
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)))
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();
1754
int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1755
uint32_t db_stat, uint32_t ha_open_flags,
1757
bool &error_reported)
1759
1728
/* The table struct is now initialized; Open the table */
1763
1732
assert(!(db_stat & HA_WAIT_IF_LOCKED));
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))))
1770
1739
switch (ha_err)
1958
1922
false /* is_unsigned */);
1959
case DRIZZLE_TYPE_UUID:
1960
return new (&mem_root) field::Uuid(ptr,
1965
case DRIZZLE_TYPE_BOOLEAN:
1966
return new (&mem_root) field::Boolean(ptr,
1972
1923
case DRIZZLE_TYPE_LONG:
1973
return new (&mem_root) field::Int32(ptr,
1924
return new (&mem_root) Field_long(ptr,
1931
false /* is_unsigned */);
1979
1932
case DRIZZLE_TYPE_LONGLONG:
1983
return new (&mem_root) field::Size(ptr,
1991
return new (&mem_root) field::Int64(ptr,
1998
case DRIZZLE_TYPE_MICROTIME:
1999
return new (&mem_root) field::Microtime(ptr,
1933
return new (&mem_root) Field_int64_t(ptr,
1940
false /* is_unsigned */);
2005
1941
case DRIZZLE_TYPE_TIMESTAMP:
2006
return new (&mem_root) field::Epoch(ptr,
2012
case DRIZZLE_TYPE_TIME:
2013
return new (&mem_root) field::Time(ptr,
1942
return new (&mem_root) Field_timestamp(ptr,
2018
1950
case DRIZZLE_TYPE_DATE:
2019
1951
return new (&mem_root) Field_date(ptr,
2023
1956
case DRIZZLE_TYPE_DATETIME:
2024
1957
return new (&mem_root) Field_datetime(ptr,
2028
1962
case DRIZZLE_TYPE_NULL:
2029
1963
return new (&mem_root) Field_null(ptr,
1967
default: // Impossible (Wrong version)
2037
void TableShare::refreshVersion()
2039
version= refresh_version;