44
44
#include "drizzled/internal/my_pthread.h"
45
45
#include "drizzled/plugin/event_observer.h"
47
#include "drizzled/table.h"
48
#include "drizzled/table/shell.h"
50
47
#include "drizzled/session.h"
52
49
#include "drizzled/charset.h"
70
67
#include "drizzled/field/decimal.h"
71
68
#include "drizzled/field/real.h"
72
69
#include "drizzled/field/double.h"
73
#include "drizzled/field/int32.h"
74
#include "drizzled/field/int64.h"
70
#include "drizzled/field/long.h"
71
#include "drizzled/field/int64_t.h"
75
72
#include "drizzled/field/num.h"
76
73
#include "drizzled/field/timestamp.h"
77
74
#include "drizzled/field/datetime.h"
78
75
#include "drizzled/field/varstring.h"
79
#include "drizzled/field/uuid.h"
81
#include "drizzled/definition/cache.h"
83
77
using namespace std;
88
82
extern size_t table_def_size;
83
static TableDefinitionCache table_def_cache;
84
static pthread_mutex_t LOCK_table_share;
85
bool table_def_inited= false;
90
87
/*****************************************************************************
91
88
Functions to handle table definition cach (TableShare)
92
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());
95
125
Mark that we are not using table share anymore.
106
136
void TableShare::release(TableShare *share)
108
138
bool to_be_deleted= false;
109
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
112
if (!--share->ref_count)
120
definition::Cache::singleton().erase(share->getCacheKey());
124
void TableShare::release(TableShare::shared_ptr &share)
126
bool to_be_deleted= false;
127
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
130
if (!--share->ref_count)
138
definition::Cache::singleton().erase(share->getCacheKey());
142
void TableShare::release(const TableIdentifier &identifier)
144
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
139
safe_mutex_assert_owner(&LOCK_open);
141
pthread_mutex_lock(&share->mutex);
142
if (!--share->ref_count)
149
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
150
plugin::EventObserver::deregisterTableEvents(*share);
152
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
153
if (iter != table_def_cache.end())
155
table_def_cache.erase(iter);
160
pthread_mutex_unlock(&share->mutex);
163
void TableShare::release(TableIdentifier &identifier)
165
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
166
if (iter != table_def_cache.end())
168
TableShare *share= (*iter).second;
147
169
share->version= 0; // Mark for delete
148
170
if (share->ref_count == 0)
150
definition::Cache::singleton().erase(identifier.getKey());
172
pthread_mutex_lock(&share->mutex);
173
plugin::EventObserver::deregisterTableEvents(*share);
174
table_def_cache.erase(identifier.getKey());
156
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
181
static TableShare *foundTableShare(TableShare *share)
159
184
We found an existing table definition. Return it if we didn't get
163
188
/* We must do a lock to ensure that the structure is initialized */
189
(void) pthread_mutex_lock(&share->mutex);
164
190
if (share->error)
166
192
/* Table definition contained an error */
167
193
share->open_table_error(share->error, share->open_errno, share->errarg);
194
(void) pthread_mutex_unlock(&share->mutex);
169
return TableShare::shared_ptr();
172
199
share->incrementTableCount();
200
(void) pthread_mutex_unlock(&share->mutex);
197
225
# Share for table
200
TableShare::shared_ptr TableShare::getShareCreate(Session *session,
201
const TableIdentifier &identifier,
228
TableShare *TableShare::getShareCreate(Session *session,
229
TableIdentifier &identifier,
204
TableShare::shared_ptr share;
232
TableShare *share= NULL;
208
236
/* Read table definition from cache */
209
if ((share= definition::Cache::singleton().find(identifier.getKey())))
237
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
238
if (iter != table_def_cache.end())
240
share= (*iter).second;
210
241
return foundTableShare(share);
212
share.reset(new TableShare(message::Table::STANDARD, identifier));
244
if (not (share= new TableShare(message::Table::STANDARD, identifier)))
250
Lock mutex to be able to read table definition from file without
253
(void) pthread_mutex_lock(&share->mutex);
256
* @TODO: we need to eject something if we exceed table_def_size
258
pair<TableDefinitionCache::iterator, bool> ret=
259
table_def_cache.insert(make_pair(identifier.getKey(), share));
260
if (ret.second == false)
214
267
if (share->open_table_def(*session, identifier))
216
in_error= share->error;
269
*error= share->error;
270
table_def_cache.erase(identifier.getKey());
218
return TableShare::shared_ptr();
220
275
share->ref_count++; // Mark in use
222
277
plugin::EventObserver::registerTableEvents(*share);
224
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
227
return TableShare::shared_ptr();
279
(void) pthread_mutex_unlock(&share->mutex);
286
Check if table definition exits in cache
289
get_cached_table_share()
291
table_name Table name
295
# TableShare for table
297
TableShare *TableShare::getShare(TableIdentifier &identifier)
299
safe_mutex_assert_owner(&LOCK_open);
301
TableDefinitionCache::iterator iter= table_def_cache.find(identifier.getKey());
302
if (iter != table_def_cache.end())
304
return (*iter).second;
312
/* Get column name from column hash */
314
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
316
*length= (uint32_t) strlen((*buff)->field_name);
317
return (unsigned char*) (*buff)->field_name;
232
320
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
234
322
enum_field_types field_type;
265
353
case message::Table::Field::BLOB:
266
354
field_type= DRIZZLE_TYPE_BLOB;
268
case message::Table::Field::UUID:
269
field_type= DRIZZLE_TYPE_UUID;
273
abort(); // Programming error
357
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
276
361
return field_type;
304
389
default_value->length());
306
391
case DRIZZLE_TYPE_NULL:
309
393
case DRIZZLE_TYPE_TIMESTAMP:
310
394
case DRIZZLE_TYPE_DATETIME:
311
395
case DRIZZLE_TYPE_DATE:
396
if (default_value->compare("NOW()") == 0)
312
398
case DRIZZLE_TYPE_ENUM:
313
case DRIZZLE_TYPE_UUID:
314
399
default_item= new Item_string(default_value->c_str(),
315
400
default_value->length(),
316
401
system_charset_info);
372
TableShare::TableShare(const TableIdentifier::Type type_arg) :
457
const TableDefinitionCache &TableShare::getCache()
459
return table_def_cache;
462
TableShare::TableShare(TableIdentifier::Type type_arg) :
373
463
table_category(TABLE_UNKNOWN_CATEGORY),
374
465
found_next_number_field(NULL),
375
466
timestamp_field(NULL),
377
mem_root(TABLE_ALLOC_BLOCK_SIZE),
381
471
timestamp_offset(0),
383
473
stored_rec_length(0),
474
row_type(ROW_TYPE_DEFAULT),
385
476
table_proto(NULL),
386
477
storage_engine(NULL),
505
column_bitmap_size(0),
413
506
blob_ptr_size(0),
414
507
db_low_byte_first(false),
509
replace_with_name_lock(false),
510
waiting_on_cond(false),
416
512
keys_for_keyread(0),
417
event_observers(NULL)
513
event_observers(NULL),
516
memset(&name_hash, 0, sizeof(HASH));
420
518
table_charset= 0;
421
519
memset(&db, 0, sizeof(LEX_STRING));
426
524
if (type_arg == message::Table::INTERNAL)
428
TableIdentifier::build_tmptable_filename(private_key_for_cache.vectorPtr());
429
init(private_key_for_cache.vector(), private_key_for_cache.vector());
526
TableIdentifier::build_tmptable_filename(private_key_for_cache);
527
init(&private_key_for_cache[0], &private_key_for_cache[0]);
437
TableShare::TableShare(const TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
535
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
438
536
table_category(TABLE_UNKNOWN_CATEGORY),
439
538
found_next_number_field(NULL),
440
539
timestamp_field(NULL),
442
mem_root(TABLE_ALLOC_BLOCK_SIZE),
446
544
timestamp_offset(0),
448
546
stored_rec_length(0),
547
row_type(ROW_TYPE_DEFAULT),
450
549
table_proto(NULL),
451
550
storage_engine(NULL),
578
column_bitmap_size(0),
478
579
blob_ptr_size(0),
479
580
db_low_byte_first(false),
582
replace_with_name_lock(false),
583
waiting_on_cond(false),
481
585
keys_for_keyread(0),
482
event_observers(NULL)
586
event_observers(NULL),
589
memset(&name_hash, 0, sizeof(HASH));
484
591
assert(identifier.getKey() == key);
486
593
table_charset= 0;
490
597
private_key_for_cache= key;
599
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
492
600
table_category= TABLE_CATEGORY_TEMPORARY;
493
601
tmp_table= message::Table::INTERNAL;
495
db.str= const_cast<char *>(private_key_for_cache.vector());
496
db.length= strlen(private_key_for_cache.vector());
603
db.str= &private_key_for_cache[0];
604
db.length= strlen(&private_key_for_cache[0]);
498
table_name.str= const_cast<char *>(private_key_for_cache.vector()) + strlen(private_key_for_cache.vector()) + 1;
606
table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
499
607
table_name.length= strlen(table_name.str);
500
608
path.str= (char *)"";
501
609
normalized_path.str= path.str;
502
610
path.length= normalized_path.length= 0;
504
std::string tb_name(identifier.getTableName());
505
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
506
assert(strcmp(tb_name.c_str(), table_name.str) == 0);
611
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
508
612
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
512
616
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
513
617
table_category(TABLE_UNKNOWN_CATEGORY),
514
619
found_next_number_field(NULL),
515
620
timestamp_field(NULL),
517
mem_root(TABLE_ALLOC_BLOCK_SIZE),
521
625
timestamp_offset(0),
523
627
stored_rec_length(0),
628
row_type(ROW_TYPE_DEFAULT),
525
630
table_proto(NULL),
526
631
storage_engine(NULL),
659
column_bitmap_size(0),
553
660
blob_ptr_size(0),
554
661
db_low_byte_first(false),
663
replace_with_name_lock(false),
664
waiting_on_cond(false),
556
666
keys_for_keyread(0),
557
event_observers(NULL)
667
event_observers(NULL),
670
memset(&name_hash, 0, sizeof(HASH));
559
672
table_charset= 0;
560
673
memset(&db, 0, sizeof(LEX_STRING));
561
674
memset(&table_name, 0, sizeof(LEX_STRING));
568
681
memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
684
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
571
685
table_category= TABLE_CATEGORY_TEMPORARY;
572
686
tmp_table= message::Table::INTERNAL;
573
db.str= const_cast<char *>(private_key_for_cache.vector());
574
db.length= strlen(private_key_for_cache.vector());
687
db.str= &private_key_for_cache[0];
688
db.length= strlen(&private_key_for_cache[0]);
575
689
table_name.str= db.str + 1;
576
690
table_name.length= strlen(table_name.str);
577
691
path.str= &private_normalized_path[0];
585
699
Used for shares that will go into the cache.
587
TableShare::TableShare(const TableIdentifier::Type type_arg,
588
const TableIdentifier &identifier,
701
TableShare::TableShare(TableIdentifier::Type type_arg,
702
TableIdentifier &identifier,
590
704
uint32_t path_length_arg) :
591
705
table_category(TABLE_UNKNOWN_CATEGORY),
592
707
found_next_number_field(NULL),
593
708
timestamp_field(NULL),
595
mem_root(TABLE_ALLOC_BLOCK_SIZE),
599
713
timestamp_offset(0),
601
715
stored_rec_length(0),
716
row_type(ROW_TYPE_DEFAULT),
603
718
table_proto(NULL),
604
719
storage_engine(NULL),
747
column_bitmap_size(0),
631
748
blob_ptr_size(0),
632
749
db_low_byte_first(false),
751
replace_with_name_lock(false),
752
waiting_on_cond(false),
634
754
keys_for_keyread(0),
635
event_observers(NULL)
755
event_observers(NULL),
758
memset(&name_hash, 0, sizeof(HASH));
637
760
table_charset= 0;
638
761
memset(&db, 0, sizeof(LEX_STRING));
639
762
memset(&table_name, 0, sizeof(LEX_STRING));
640
763
memset(&path, 0, sizeof(LEX_STRING));
641
764
memset(&normalized_path, 0, sizeof(LEX_STRING));
766
mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
644
768
std::string _path;
648
772
Let us use the fact that the key is "db/0/table_name/0" + optional
649
773
part for temporary tables.
651
db.str= const_cast<char *>(private_key_for_cache.vector());
775
db.str= &private_key_for_cache[0];
652
776
db.length= strlen(db.str);
653
777
table_name.str= db.str + db.length + 1;
654
778
table_name.length= strlen(table_name.str);
662
786
TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
665
if ((path_buff= (char *)mem_root.alloc_root(_path.length() + 1)))
789
if (mem_root.multi_alloc_root(0,
790
&path_buff, _path.length() + 1,
667
793
setPath(path_buff, _path.length());
668
794
strcpy(path_buff, _path.c_str());
669
795
setNormalizedPath(path_buff, _path.length());
671
797
version= refresh_version;
799
pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
800
pthread_cond_init(&cond, NULL);
675
804
assert(0); // We should throw here.
680
810
void TableShare::init(const char *new_table_name,
681
811
const char *new_path)
814
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
684
815
table_category= TABLE_CATEGORY_TEMPORARY;
685
816
tmp_table= message::Table::INTERNAL;
686
817
db.str= (char *)"";
697
828
assert(ref_count == 0);
831
If someone is waiting for this to be deleted, inform it about this.
832
Don't do a delete until we know that no one is refering to this anymore.
834
if (tmp_table == message::Table::STANDARD)
836
/* share->mutex is locked in release_table_share() */
837
while (waiting_on_cond)
839
pthread_cond_broadcast(&cond);
840
pthread_cond_wait(&cond, &mutex);
842
/* No thread refers to this anymore */
843
pthread_mutex_unlock(&mutex);
844
pthread_mutex_destroy(&mutex);
845
pthread_cond_destroy(&cond);
847
hash_free(&name_hash);
699
849
storage_engine= NULL;
701
851
delete table_proto;
702
852
table_proto= NULL;
704
plugin::EventObserver::deregisterTableEvents(*this);
706
854
mem_root.free_root(MYF(0)); // Free's share
709
void TableShare::setIdentifier(const TableIdentifier &identifier_arg)
857
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
859
private_key_for_cache.clear();
711
860
private_key_for_cache= identifier_arg.getKey();
714
863
Let us use the fact that the key is "db/0/table_name/0" + optional
715
864
part for temporary tables.
717
db.str= const_cast<char *>(private_key_for_cache.vector());
866
db.str= &private_key_for_cache[0];
718
867
db.length= strlen(db.str);
719
868
table_name.str= db.str + db.length + 1;
720
869
table_name.length= strlen(table_name.str);
754
903
db_create_options= (local_db_create_options & 0x0000FFFF);
755
904
db_options_in_use= db_create_options;
906
row_type= table_options.has_row_type() ?
907
(enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
757
909
block_size= table_options.has_block_size() ?
758
910
table_options.block_size() : 0;
760
table_charset= get_charset(table_options.collation_id());
912
table_charset= get_charset(table_options.has_collation_id()?
913
table_options.collation_id() : 0);
765
snprintf(errmsg, sizeof(errmsg),
766
_("Table %s has invalid/unknown collation: %d,%s"),
768
table_options.collation_id(),
769
table_options.collation().c_str());
772
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
773
return ER_CORRUPT_TABLE_DEFINITION;
917
/* unknown charset in head[38] or pre-3.23 frm */
918
if (use_mb(default_charset_info))
920
/* Warn that we may be changing the size of character columns */
921
errmsg_printf(ERRMSG_LVL_WARN,
922
_("'%s' had no or invalid character set, "
923
"and default character set is multi-byte, "
924
"so character column sizes may have changed"),
927
table_charset= default_charset_info;
776
930
db_record_offset= 1;
883
1037
key_part->length= part.compare_length();
887
if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
888
|| table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
890
uint32_t collation_id;
892
if (table.field(part.fieldnr()).string_options().has_collation_id())
893
collation_id= table.field(part.fieldnr()).string_options().collation_id();
895
collation_id= table.options().collation_id();
897
const CHARSET_INFO *cs= get_charset(collation_id);
899
mbmaxlen= cs->mbmaxlen;
901
key_part->length*= mbmaxlen;
903
1039
key_part->store_length= key_part->length;
905
1041
/* key_part->offset is set later */
934
1070
uint32_t local_null_fields= 0;
937
std::vector<uint32_t> field_offsets;
938
std::vector<uint32_t> field_pack_length;
1073
vector<uint32_t> field_offsets;
1074
vector<uint32_t> field_pack_length;
940
1076
field_offsets.resize(fields);
941
1077
field_pack_length.resize(fields);
982
1118
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
984
field_pack_length[fieldnr]= 4;
1120
field_pack_length[fieldnr]=
1121
get_enum_pack_length(field_options.field_value_size());
986
1123
interval_count++;
987
1124
interval_parts+= field_options.field_value_size();
1107
1244
bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
1247
use_hash= ! hash_init(&name_hash,
1248
system_charset_info,
1252
(hash_get_key) get_field_name,
1109
1256
unsigned char* null_pos= getDefaultValues();
1110
1257
int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1124
1271
if (pfield.has_options() &&
1125
pfield.options().has_default_expression() &&
1126
pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
1272
pfield.options().has_default_value() &&
1273
pfield.options().default_value().compare("NOW()") == 0)
1128
if (pfield.options().has_update_expression() &&
1129
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1275
if (pfield.options().has_update_value() &&
1276
pfield.options().update_value().compare("NOW()") == 0)
1131
1278
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
1133
else if (! pfield.options().has_update_expression())
1280
else if (! pfield.options().has_update_value())
1135
1282
unireg_type= Field::TIMESTAMP_DN_FIELD;
1139
assert(0); // Invalid update value.
1285
assert(1); // Invalid update value.
1143
1287
else if (pfield.has_options() &&
1144
pfield.options().has_update_expression() &&
1145
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1288
pfield.options().has_update_value() &&
1289
pfield.options().update_value().compare("NOW()") == 0)
1147
1291
unireg_type= Field::TIMESTAMP_UN_FIELD;
1220
1364
Item *default_value= NULL;
1222
1366
if (pfield.options().has_default_value() ||
1223
pfield.options().default_null() ||
1367
pfield.options().has_default_null() ||
1224
1368
pfield.options().has_default_bin_value())
1226
1370
default_value= default_value_item(field_type,
1234
blob_ptr_size= portable_sizeof_char_ptr;
1378
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
1379
memset(&temp_table, 0, sizeof(temp_table));
1380
temp_table.setShare(this);
1381
temp_table.in_use= &session;
1382
temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
1383
temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1236
1385
uint32_t field_length= 0; //Assignment is for compiler complaint.
1315
1464
case DRIZZLE_TYPE_LONGLONG:
1316
1465
field_length= MAX_BIGINT_WIDTH;
1318
case DRIZZLE_TYPE_UUID:
1319
field_length= field::Uuid::max_string_length();
1321
1467
case DRIZZLE_TYPE_NULL:
1322
1468
abort(); // Programming error
1325
assert(enum_field_types_size == 12);
1327
1471
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1329
pfield.constraints().is_nullable(),
1335
MTYP_TYPENR(unireg_type),
1336
((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1337
getTableProto()->field(fieldnr).name().c_str());
1473
pfield.constraints().is_nullable(),
1479
(Field::utype) MTYP_TYPENR(unireg_type),
1480
((field_type == DRIZZLE_TYPE_ENUM) ?
1481
&intervals[interval_nr++]
1483
getTableProto()->field(fieldnr).name().c_str());
1339
1485
field[fieldnr]= f;
1341
// Insert post make_field code here.
1344
case DRIZZLE_TYPE_BLOB:
1345
case DRIZZLE_TYPE_VARCHAR:
1346
case DRIZZLE_TYPE_DOUBLE:
1347
case DRIZZLE_TYPE_DECIMAL:
1348
case DRIZZLE_TYPE_TIMESTAMP:
1349
case DRIZZLE_TYPE_DATETIME:
1350
case DRIZZLE_TYPE_DATE:
1351
case DRIZZLE_TYPE_ENUM:
1352
case DRIZZLE_TYPE_LONG:
1353
case DRIZZLE_TYPE_LONGLONG:
1354
case DRIZZLE_TYPE_NULL:
1355
case DRIZZLE_TYPE_UUID:
1359
// This needs to go, we should be setting the "use" on the field so that
1360
// it does not reference the share/table.
1361
table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1362
temp_table.in_use= &session;
1364
1487
f->init(&temp_table); /* blob default values need table obj */
1366
1489
if (! (f->flags & NOT_NULL_FLAG))
1412
1536
if (f->unireg_check == Field::NEXT_NUMBER)
1413
1537
found_next_number_field= &(field[fieldnr]);
1539
if (timestamp_field == f)
1540
timestamp_field_offset= fieldnr;
1415
1542
if (use_hash) /* supposedly this never fails... but comments lie */
1417
const char *local_field_name= field[fieldnr]->field_name;
1418
name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1543
(void) my_hash_insert(&name_hash,
1544
(unsigned char*)&(field[fieldnr]));
1422
1548
keyinfo= key_info;
1607
1734
if (blob_fields)
1609
1738
/* Store offsets to blob fields to find them fast */
1610
1739
blob_field.resize(blob_fields);
1611
uint32_t *save= &blob_field[0];
1740
save= &blob_field[0];
1613
1742
for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1615
1744
if ((*iter)->flags & BLOB_FLAG)
1731
1864
5 Error (see open_table_error: charset unavailable)
1732
1865
7 Table definition has changed in engine
1734
1868
int TableShare::open_table_from_share(Session *session,
1735
1869
const TableIdentifier &identifier,
1736
1870
const char *alias,
1737
1871
uint32_t db_stat, uint32_t ha_open_flags,
1738
1872
Table &outparam)
1875
uint32_t records, bitmap_size;
1740
1876
bool error_reported= false;
1741
int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1744
ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1749
if (not error_reported)
1750
open_table_error(ret, errno, 0);
1752
delete outparam.cursor;
1753
outparam.cursor= 0; // For easier error checking
1754
outparam.db_stat= 0;
1755
outparam.getMemRoot()->free_root(MYF(0)); // Safe to call on zeroed root
1756
outparam.clearAlias();
1761
int TableShare::open_table_from_share_inner(Session *session,
1768
unsigned char *record= NULL;
1877
unsigned char *record, *bitmaps;
1769
1878
Field **field_ptr;
1771
1880
/* Parsing of partitioning information from .frm needs session->lex set up. */
1774
1883
local_error= 1;
1775
1884
outparam.resetTable(session, this, db_stat);
1777
outparam.setAlias(alias);
1887
if (not (outparam.alias= strdup(alias)))
1779
1890
/* Allocate Cursor */
1780
if (not (outparam.cursor= db_type()->getCursor(outparam)))
1891
if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
1783
1894
local_error= 4;
1801
1912
if (records > 1)
1802
1913
outparam.record[1]= record+ rec_buff_length;
1804
outparam.record[1]= outparam.getInsertRecord(); // Safety
1915
outparam.record[1]= outparam.record[0]; // Safety
1807
#ifdef HAVE_VALGRIND
1809
1920
We need this because when we read var-length rows, we are not updating
1810
1921
bytes after end of varchar
1812
1923
if (records > 1)
1814
memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1815
memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1925
memcpy(outparam.record[0], getDefaultValues(), rec_buff_length);
1926
memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1816
1927
if (records > 2)
1817
memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1928
memcpy(outparam.record[1], getDefaultValues(), rec_buff_length);
1820
1931
if (records > 1)
1822
memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1933
memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1825
1936
if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1830
1941
outparam.setFields(field_ptr);
1832
record= (unsigned char*) outparam.getInsertRecord()-1; /* Fieldstart = 1 */
1943
record= (unsigned char*) outparam.record[0]-1; /* Fieldstart = 1 */
1834
1945
outparam.null_flags= (unsigned char*) record+1;
1845
1956
outparam.found_next_number_field=
1846
1957
outparam.getField(positionFields(found_next_number_field));
1847
1958
if (timestamp_field)
1848
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field->position());
1959
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1850
1962
/* Fix key->name and key_part->field */
1895
2007
/* Allocate bitmaps */
1897
outparam.def_read_set.resize(fields);
1898
outparam.def_write_set.resize(fields);
1899
outparam.tmp_set.resize(fields);
2009
bitmap_size= column_bitmap_size;
2010
if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
2014
outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
2015
outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
2016
outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1900
2017
outparam.default_column_bitmaps();
1905
int TableShare::open_table_cursor_inner(const TableIdentifier &identifier,
1906
uint32_t db_stat, uint32_t ha_open_flags,
1908
bool &error_reported)
1910
2019
/* The table struct is now initialized; Open the table */
1914
2023
assert(!(db_stat & HA_WAIT_IF_LOCKED));
1917
if ((ha_err= (outparam.cursor->ha_open(identifier,
1918
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1919
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
2026
if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam, getNormalizedPath(),
2027
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
2028
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1921
2030
switch (ha_err)
1943
2052
local_error= 7;
2059
#if defined(HAVE_purify)
2060
memset(bitmaps, 0, bitmap_size*3);
2066
if (!error_reported)
2067
open_table_error(local_error, errno, 0);
2069
delete outparam.cursor;
2070
outparam.cursor= 0; // For easier error checking
2071
outparam.db_stat= 0;
2072
outparam.getMemRoot()->free_root(MYF(0)); // Safe to call on zeroed root
2073
free((char*) outparam.alias);
2074
return (local_error);
1953
2077
/* error message when opening a form cursor */
2176
get_enum_pack_length(interval->count),
2054
2178
field_charset);
2055
2179
case DRIZZLE_TYPE_VARCHAR:
2057
2180
return new (&mem_root) Field_varstring(ptr,field_length,
2058
ha_varchar_packlength(field_length),
2181
HA_VARCHAR_PACKLENGTH(field_length),
2059
2182
null_pos,null_bit,
2061
2185
field_charset);
2062
2186
case DRIZZLE_TYPE_BLOB:
2063
2187
return new (&mem_root) Field_blob(ptr,
2088
2213
false /* is_unsigned */);
2089
case DRIZZLE_TYPE_UUID:
2090
return new (&mem_root) field::Uuid(ptr,
2095
2214
case DRIZZLE_TYPE_LONG:
2096
return new (&mem_root) field::Int32(ptr,
2215
return new (&mem_root) Field_long(ptr,
2222
false /* is_unsigned */);
2102
2223
case DRIZZLE_TYPE_LONGLONG:
2103
return new (&mem_root) field::Int64(ptr,
2224
return new (&mem_root) Field_int64_t(ptr,
2231
false /* is_unsigned */);
2109
2232
case DRIZZLE_TYPE_TIMESTAMP:
2110
2233
return new (&mem_root) Field_timestamp(ptr,