drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
base.cc
1/* -*- mode: c++; c-basic-offset: 2; i/dent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 * Copyright (C) 2009 Sun Microsystems, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
23 This class is shared between different table objects. There is one
24 instance of table share per one table in the database.
25*/
26
27/* Basic functions needed by many modules */
28#include <config.h>
29
30#include <pthread.h>
31#include <float.h>
32
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36
37
38#include <cassert>
39
40#include <drizzled/error.h>
41#include <drizzled/gettext.h>
42#include <drizzled/sql_base.h>
43#include <drizzled/pthread_globals.h>
44#include <drizzled/internal/my_pthread.h>
45
46#include <drizzled/table.h>
47#include <drizzled/table/shell.h>
48
49#include <drizzled/session.h>
50
51#include <drizzled/charset.h>
52#include <drizzled/internal/m_string.h>
53#include <drizzled/internal/my_sys.h>
54
55#include <drizzled/item/string.h>
56#include <drizzled/item/int.h>
57#include <drizzled/item/decimal.h>
58#include <drizzled/item/float.h>
59#include <drizzled/item/null.h>
60#include <drizzled/temporal.h>
61
62#include <drizzled/field.h>
63#include <drizzled/field/str.h>
64#include <drizzled/field/num.h>
65#include <drizzled/field/blob.h>
66#include <drizzled/field/boolean.h>
67#include <drizzled/field/enum.h>
68#include <drizzled/field/null.h>
69#include <drizzled/field/date.h>
70#include <drizzled/field/decimal.h>
71#include <drizzled/field/real.h>
72#include <drizzled/field/double.h>
73#include <drizzled/field/int32.h>
74#include <drizzled/field/int64.h>
75#include <drizzled/field/size.h>
76#include <drizzled/field/num.h>
77#include <drizzled/field/time.h>
78#include <drizzled/field/epoch.h>
79#include <drizzled/field/datetime.h>
80#include <drizzled/field/microtime.h>
81#include <drizzled/field/varstring.h>
82#include <drizzled/field/uuid.h>
83#include <drizzled/field/ipv6.h>
84#include <drizzled/plugin/storage_engine.h>
85#include <drizzled/definition/cache.h>
86#include <drizzled/typelib.h>
87#include <drizzled/key.h>
88#include <drizzled/open_tables_state.h>
89#include <drizzled/catalog/local.h>
90#include <drizzled/key_part_info.h>
91
92using namespace std;
93
94namespace drizzled {
95
96extern size_t table_def_size;
97
98static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
99{
100 switch(field.type())
101 {
102 case message::Table::Field::INTEGER:
103 return DRIZZLE_TYPE_LONG;
104
105 case message::Table::Field::DOUBLE:
106 return DRIZZLE_TYPE_DOUBLE;
107
108 case message::Table::Field::EPOCH:
109 if (field.has_time_options() and field.time_options().microseconds())
110 return DRIZZLE_TYPE_MICROTIME;
111
112 return DRIZZLE_TYPE_TIMESTAMP;
113
114 case message::Table::Field::BIGINT:
115 return DRIZZLE_TYPE_LONGLONG;
116
117 case message::Table::Field::DATETIME:
118 return DRIZZLE_TYPE_DATETIME;
119
120 case message::Table::Field::DATE:
121 return DRIZZLE_TYPE_DATE;
122
123 case message::Table::Field::VARCHAR:
124 return DRIZZLE_TYPE_VARCHAR;
125
126 case message::Table::Field::DECIMAL:
127 return DRIZZLE_TYPE_DECIMAL;
128
129 case message::Table::Field::ENUM:
130 return DRIZZLE_TYPE_ENUM;
131
132 case message::Table::Field::BLOB:
133 return DRIZZLE_TYPE_BLOB;
134
135 case message::Table::Field::UUID:
136 return DRIZZLE_TYPE_UUID;
137
138 case message::Table::Field::IPV6:
139 return DRIZZLE_TYPE_IPV6;
140
141 case message::Table::Field::BOOLEAN:
142 return DRIZZLE_TYPE_BOOLEAN;
143
144 case message::Table::Field::TIME:
145 return DRIZZLE_TYPE_TIME;
146 }
147
148 abort();
149}
150
151static Item* default_value_item(enum_field_types field_type, const charset_info_st& charset, bool default_null,
152 const string& default_value, const string& default_bin_value)
153{
154 if (default_null)
155 return new Item_null();
156
157 switch (field_type)
158 {
159 case DRIZZLE_TYPE_LONG:
160 case DRIZZLE_TYPE_LONGLONG:
161 {
162 int error= 0;
163 int64_t val= (int64_t) internal::my_strtoll10(default_value.c_str(), NULL, &error);
164 if (error && error != -1) /* was an error and wasn't a negative number */
165 return NULL;
166 return new Item_int(default_value.c_str(), val, default_value.length());
167 }
168 case DRIZZLE_TYPE_DOUBLE:
169 return new Item_float(default_value.c_str(), default_value.length());
170 case DRIZZLE_TYPE_NULL:
171 assert(false);
172 abort();
173 case DRIZZLE_TYPE_TIMESTAMP:
174 case DRIZZLE_TYPE_DATETIME:
175 case DRIZZLE_TYPE_TIME:
176 case DRIZZLE_TYPE_DATE:
177 case DRIZZLE_TYPE_ENUM:
178 case DRIZZLE_TYPE_UUID:
179 case DRIZZLE_TYPE_IPV6:
180 case DRIZZLE_TYPE_MICROTIME:
181 case DRIZZLE_TYPE_BOOLEAN:
182 // return new Item_string(*default_value, system_charset_info); // crash
183 return new Item_string(default_value.data(), default_value.size(), system_charset_info);
184 case DRIZZLE_TYPE_VARCHAR:
185 case DRIZZLE_TYPE_BLOB: /* Blob is here due to TINYTEXT. Feel the hate. */
186 return &charset== &my_charset_bin
187 ? new Item_string(default_bin_value, &my_charset_bin)
188 : new Item_string(default_value, system_charset_info);
189 case DRIZZLE_TYPE_DECIMAL:
190 return new Item_decimal(default_value.c_str(), default_value.length(), system_charset_info);
191 }
192 return NULL;
193}
194
195
196
203{
204 assert(getTableMessage());
205
206 size_t num_indexes= getTableMessage()->indexes_size();
207
208 for (size_t x= 0; x < num_indexes; ++x)
209 {
210 const message::Table::Index &index= getTableMessage()->indexes(x);
211 if (index.is_primary())
212 {
213 size_t num_parts= index.index_part_size();
214 for (size_t y= 0; y < num_parts; ++y)
215 {
216 if (index.index_part(y).fieldnr() == in_field->position())
217 return true;
218 }
219 }
220 }
221 return false;
222}
223
224TableShare::TableShare(const identifier::Table::Type type_arg) :
225 table_category(TABLE_UNKNOWN_CATEGORY),
226 found_next_number_field(NULL),
227 timestamp_field(NULL),
228 key_info(NULL),
229 mem_root(TABLE_ALLOC_BLOCK_SIZE),
230 all_set(),
231 table_identifier(NULL),
232 block_size(0),
233 version(0),
234 timestamp_offset(0),
235 reclength(0),
236 stored_rec_length(0),
237 max_rows(0),
238 _table_message(NULL),
239 storage_engine(NULL),
240 tmp_table(type_arg),
241 _ref_count(0),
242 null_bytes(0),
243 last_null_bit_pos(0),
244 _field_size(0),
245 rec_buff_length(0),
246 keys(0),
247 key_parts(0),
248 max_key_length(0),
249 max_unique_length(0),
250 total_key_length(0),
251 uniques(0),
252 null_fields(0),
253 blob_fields(0),
254 has_variable_width(false),
255 db_create_options(0),
256 db_options_in_use(0),
257 db_record_offset(0),
258 rowid_field_offset(0),
259 primary_key(MAX_KEY),
260 next_number_index(0),
261 next_number_key_offset(0),
262 next_number_keypart(0),
263 error(0),
264 open_errno(0),
265 errarg(0),
266 blob_ptr_size(portable_sizeof_char_ptr),
267 db_low_byte_first(false),
268 keys_in_use(0),
269 keys_for_keyread(0)
270{
271 if (type_arg == message::Table::INTERNAL)
272 {
273 string s= identifier::Table::build_tmptable_filename();
274 private_key_for_cache.vectorPtr().assign(s.c_str(), s.c_str() + s.size() + 1);
275 init(private_key_for_cache.vector(), private_key_for_cache.vector());
276 }
277 else
278 {
279 init("", "");
280 }
281}
282
283TableShare::TableShare(const identifier::Table &identifier, const identifier::Table::Key &key) :// Used by placeholder
284 table_category(TABLE_UNKNOWN_CATEGORY),
285 found_next_number_field(NULL),
286 timestamp_field(NULL),
287 key_info(NULL),
288 mem_root(TABLE_ALLOC_BLOCK_SIZE),
289 table_charset(0),
290 all_set(),
291 table_identifier(NULL),
292 block_size(0),
293 version(0),
294 timestamp_offset(0),
295 reclength(0),
296 stored_rec_length(0),
297 max_rows(0),
298 _table_message(NULL),
299 storage_engine(NULL),
300 tmp_table(message::Table::INTERNAL),
301 _ref_count(0),
302 null_bytes(0),
303 last_null_bit_pos(0),
304 _field_size(0),
305 rec_buff_length(0),
306 keys(0),
307 key_parts(0),
308 max_key_length(0),
309 max_unique_length(0),
310 total_key_length(0),
311 uniques(0),
312 null_fields(0),
313 blob_fields(0),
314 has_variable_width(false),
315 db_create_options(0),
316 db_options_in_use(0),
317 db_record_offset(0),
318 rowid_field_offset(0),
319 primary_key(MAX_KEY),
320 next_number_index(0),
321 next_number_key_offset(0),
322 next_number_keypart(0),
323 error(0),
324 open_errno(0),
325 errarg(0),
326 blob_ptr_size(portable_sizeof_char_ptr),
327 db_low_byte_first(false),
328 keys_in_use(0),
329 keys_for_keyread(0)
330{
331 table_identifier= new identifier::Table(identifier);
332
333 assert(identifier.getKey() == key);
334
335 private_key_for_cache= key;
336
338 tmp_table= message::Table::INTERNAL;
339
340 path= str_ref("");
341 normalized_path= str_ref("");
342}
343
344TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
345 table_category(TABLE_UNKNOWN_CATEGORY),
346 found_next_number_field(NULL),
347 timestamp_field(NULL),
348 key_info(NULL),
349 mem_root(TABLE_ALLOC_BLOCK_SIZE),
350 table_charset(0),
351 all_set(),
352 table_identifier(NULL),
353 block_size(0),
354 version(0),
355 timestamp_offset(0),
356 reclength(0),
357 stored_rec_length(0),
358 max_rows(0),
359 _table_message(NULL),
360 storage_engine(NULL),
361 tmp_table(identifier.getType()),
362 _ref_count(0),
363 null_bytes(0),
364 last_null_bit_pos(0),
365 _field_size(0),
366 rec_buff_length(0),
367 keys(0),
368 key_parts(0),
369 max_key_length(0),
370 max_unique_length(0),
371 total_key_length(0),
372 uniques(0),
373 null_fields(0),
374 blob_fields(0),
375 has_variable_width(false),
376 db_create_options(0),
377 db_options_in_use(0),
378 db_record_offset(0),
379 rowid_field_offset(0),
380 primary_key(MAX_KEY),
381 next_number_index(0),
382 next_number_key_offset(0),
383 next_number_keypart(0),
384 error(0),
385 open_errno(0),
386 errarg(0),
387 blob_ptr_size(portable_sizeof_char_ptr),
388 db_low_byte_first(false),
389 keys_in_use(0),
390 keys_for_keyread(0)
391{
392 table_identifier= new identifier::Table(identifier);
393
394 private_key_for_cache= identifier.getKey();
395 assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
396 private_normalized_path.resize(identifier.getPath().size() + 1);
397 memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
398
399 {
401 tmp_table= message::Table::INTERNAL;
402 path= private_normalized_path;
403 normalized_path= path;
404 }
405}
406
407
408/*
409 Used for shares that will go into the cache.
410*/
411TableShare::TableShare(const identifier::Table::Type type_arg,
412 const identifier::Table &identifier,
413 const char *path_arg,
414 uint32_t path_length_arg) :
415 table_category(TABLE_UNKNOWN_CATEGORY),
416 found_next_number_field(NULL),
417 timestamp_field(NULL),
418 key_info(NULL),
419 mem_root(TABLE_ALLOC_BLOCK_SIZE),
420 table_charset(0),
421 all_set(),
422 table_identifier(NULL),
423 block_size(0),
424 version(0),
425 timestamp_offset(0),
426 reclength(0),
427 stored_rec_length(0),
428 max_rows(0),
429 _table_message(NULL),
430 storage_engine(NULL),
431 tmp_table(type_arg),
432 _ref_count(0),
433 null_bytes(0),
434 last_null_bit_pos(0),
435 _field_size(0),
436 rec_buff_length(0),
437 keys(0),
438 key_parts(0),
439 max_key_length(0),
440 max_unique_length(0),
441 total_key_length(0),
442 uniques(0),
443 null_fields(0),
444 blob_fields(0),
445 has_variable_width(false),
446 db_create_options(0),
447 db_options_in_use(0),
448 db_record_offset(0),
449 rowid_field_offset(0),
450 primary_key(MAX_KEY),
451 next_number_index(0),
452 next_number_key_offset(0),
453 next_number_keypart(0),
454 error(0),
455 open_errno(0),
456 errarg(0),
457 blob_ptr_size(portable_sizeof_char_ptr),
458 db_low_byte_first(false),
459 keys_in_use(0),
460 keys_for_keyread(0)
461{
462 table_identifier= new identifier::Table(identifier);
463
464 private_key_for_cache= identifier.getKey();
465
466 std::string _path;
467 if (path_arg)
468 {
469 _path.assign(path_arg, path_length_arg);
470 }
471 else
472 {
473 _path= identifier::Table::build_table_filename(*table_identifier, false);
474 }
475
476 char* path_buff= mem_root.strdup(_path);
477 path= str_ref(path_buff, _path.length());
478 normalized_path= str_ref(path_buff, _path.length());
479
480 version= g_refresh_version;
481}
482
483void TableShare::init(const char *new_table_name, const char *new_path)
484{
486 tmp_table= message::Table::INTERNAL;
487
488 /* local_identifier() is okay to use here as the path is what matters */
489 identifier::Table *n= new identifier::Table(catalog::local_identifier(),
490 "", new_table_name, new_path);
491 delete table_identifier;
492 table_identifier= n;
493
494 path= str_ref(new_path);
495 normalized_path= str_ref(new_path);
496}
497
498TableShare::~TableShare()
499{
500 storage_engine= NULL;
501 delete table_identifier;
502
503 mem_root.free_root(MYF(0)); // Free's share
504}
505
506void TableShare::setIdentifier(const identifier::Table &identifier_arg)
507{
508 private_key_for_cache= identifier_arg.getKey();
509
510 delete table_identifier;
511 table_identifier= new identifier::Table(identifier_arg);
512
513 getTableMessage()->set_name(identifier_arg.getTableName());
514 getTableMessage()->set_schema(identifier_arg.getSchemaName());
515}
516
517bool TableShare::parse_table_proto(Session& session, const message::Table &table)
518{
519 drizzled::error_t local_error= EE_OK;
520
521 if (! table.IsInitialized())
522 {
523 my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
524 table.name().empty() ? " " : table.name().c_str(),
525 table.InitializationErrorString().c_str());
526
527 return true;
528 }
529
530 setTableMessage(table);
531
532 storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
533 assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
534
535 message::Table::TableOptions table_options;
536
537 if (table.has_options())
538 table_options= table.options();
539
540 uint32_t local_db_create_options= 0;
541
542 if (table_options.pack_record())
543 local_db_create_options|= HA_OPTION_PACK_RECORD;
544
545 /* local_db_create_options was stored as 2 bytes in FRM
546 Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
547 */
548 db_create_options= (local_db_create_options & 0x0000FFFF);
549 db_options_in_use= db_create_options;
550
551 block_size= table_options.has_block_size() ? table_options.block_size() : 0;
552
553 table_charset= get_charset(table_options.collation_id());
554
555 if (not table_charset)
556 {
557 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
558 table_options.collation().c_str(),
559 table.name().c_str());
560
561 return true;
562 }
563
564 db_record_offset= 1;
565
566 keys= table.indexes_size();
567
568 key_parts= 0;
569 for (int indx= 0; indx < table.indexes_size(); indx++)
570 key_parts+= table.indexes(indx).index_part_size();
571
572 key_info= (KeyInfo*) mem().alloc(table.indexes_size() * sizeof(KeyInfo) +key_parts*sizeof(KeyPartInfo));
573
574 KeyPartInfo *key_part;
575
576 key_part= reinterpret_cast<KeyPartInfo*>
577 (key_info+table.indexes_size());
578
579
580 ulong *rec_per_key= (ulong*) mem().alloc(sizeof(ulong*)*key_parts);
581
582 KeyInfo* keyinfo= key_info;
583 for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
584 {
585 message::Table::Index indx= table.indexes(keynr);
586
587 keyinfo->table= 0;
588 keyinfo->flags= 0;
589
590 if (indx.is_unique())
591 keyinfo->flags|= HA_NOSAME;
592
593 if (indx.has_options())
594 {
595 message::Table::Index::Options indx_options= indx.options();
596 if (indx_options.pack_key())
597 keyinfo->flags|= HA_PACK_KEY;
598
599 if (indx_options.var_length_key())
600 keyinfo->flags|= HA_VAR_LENGTH_PART;
601
602 if (indx_options.null_part_key())
603 keyinfo->flags|= HA_NULL_PART_KEY;
604
605 if (indx_options.binary_pack_key())
606 keyinfo->flags|= HA_BINARY_PACK_KEY;
607
608 if (indx_options.has_partial_segments())
609 keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
610
611 if (indx_options.auto_generated_key())
612 keyinfo->flags|= HA_GENERATED_KEY;
613
614 if (indx_options.has_key_block_size())
615 {
616 keyinfo->flags|= HA_USES_BLOCK_SIZE;
617 keyinfo->block_size= indx_options.key_block_size();
618 }
619 else
620 {
621 keyinfo->block_size= 0;
622 }
623 }
624
625 keyinfo->algorithm= indx.type();
626
627 keyinfo->key_length= indx.key_length();
628
629 keyinfo->key_parts= indx.index_part_size();
630
631 keyinfo->key_part= key_part;
632 keyinfo->rec_per_key= rec_per_key;
633
634 for (unsigned int partnr= 0;
635 partnr < keyinfo->key_parts;
636 partnr++, key_part++)
637 {
638 message::Table::Index::IndexPart part;
639 part= indx.index_part(partnr);
640
641 *rec_per_key++= 0;
642
643 key_part->field= NULL;
644 key_part->fieldnr= part.fieldnr() + 1; // start from 1.
645 key_part->null_bit= 0;
646 /* key_part->null_offset is only set if null_bit (see later) */
647 /* key_part->key_type= */ /* I *THINK* this may be okay.... */
648 /* key_part->type ???? */
649 key_part->key_part_flag= 0;
650 if (part.has_in_reverse_order())
651 key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
652
653 key_part->length= part.compare_length();
654
655 int mbmaxlen= 1;
656
657 if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
658 || table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
659 {
660 uint32_t collation_id;
661
662 if (table.field(part.fieldnr()).string_options().has_collation_id())
663 collation_id= table.field(part.fieldnr()).string_options().collation_id();
664 else
665 collation_id= table.options().collation_id();
666
667 const charset_info_st *cs= get_charset(collation_id);
668
669 mbmaxlen= cs->mbmaxlen;
670 }
671 key_part->length*= mbmaxlen;
672
673 key_part->store_length= key_part->length;
674
675 /* key_part->offset is set later */
676 key_part->key_type= 0;
677 }
678
679 if (not indx.has_comment())
680 {
681 keyinfo->comment.clear();
682 }
683 else
684 {
685 keyinfo->flags|= HA_USES_COMMENT;
686 keyinfo->comment.assign(mem().strdup(indx.comment()), indx.comment().length());
687 }
688
689 keyinfo->name= mem().strdup(indx.name());
690
691 addKeyName(string(keyinfo->name, indx.name().length()));
692 }
693
694 keys_for_keyread.reset();
695 set_prefix(keys_in_use, keys);
696
697 _field_size= table.field_size();
698
699 setFields(_field_size + 1);
700 _fields[_field_size]= NULL;
701
702 uint32_t local_null_fields= 0;
703 reclength= 0;
704
705 std::vector<uint32_t> field_offsets;
706 std::vector<uint32_t> field_pack_length;
707
708 field_offsets.resize(_field_size);
709 field_pack_length.resize(_field_size);
710
711 uint32_t interval_count= 0;
712
713 uint32_t stored_columns_reclength= 0;
714
715 for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
716 {
717 message::Table::Field pfield= table.field(fieldnr);
718 if (pfield.constraints().is_nullable()) // Historical reference
719 {
720 local_null_fields++;
721 }
722 else if (not pfield.constraints().is_notnull())
723 {
724 local_null_fields++;
725 }
726
727 enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
728
729 field_offsets[fieldnr]= stored_columns_reclength;
730
731 /* the below switch is very similar to
732 CreateField::create_length_to_internal_length in field.cc
733 (which should one day be replace by just this code)
734 */
735 switch(drizzle_field_type)
736 {
737 case DRIZZLE_TYPE_BLOB:
738 case DRIZZLE_TYPE_VARCHAR:
739 {
740 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
741
742 const charset_info_st *cs= get_charset(field_options.has_collation_id() ?
743 field_options.collation_id() : 0);
744
745 if (! cs)
746 cs= default_charset_info;
747
748 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
749 field_options.length() * cs->mbmaxlen);
750 }
751 break;
752 case DRIZZLE_TYPE_ENUM:
753 {
754 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
755
756 field_pack_length[fieldnr]= 4;
757
758 interval_count++;
759 }
760 break;
761 case DRIZZLE_TYPE_DECIMAL:
762 {
763 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
764
765 field_pack_length[fieldnr]= class_decimal_get_binary_size(fo.precision(), fo.scale());
766 }
767 break;
768 default:
769 /* Zero is okay here as length is fixed for other types. */
770 field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
771 }
772
773 reclength+= field_pack_length[fieldnr];
774 stored_columns_reclength+= field_pack_length[fieldnr];
775 }
776
777 /* data_offset added to stored_rec_length later */
778 stored_rec_length= stored_columns_reclength;
779
780 null_fields= local_null_fields;
781
782 ulong null_bits= local_null_fields;
783 if (! table_options.pack_record())
784 null_bits++;
785 ulong data_offset= (null_bits + 7)/8;
786
787
788 reclength+= data_offset;
789 stored_rec_length+= data_offset;
790
791 ulong local_rec_buff_length;
792
793 local_rec_buff_length= ALIGN_SIZE(reclength + 1);
794 rec_buff_length= local_rec_buff_length;
795
796 resizeDefaultValues(local_rec_buff_length);
797 unsigned char* record= getDefaultValues();
798 int null_count= 0;
799
800 if (! table_options.pack_record())
801 {
802 null_count++; // one bit for delete mark.
803 *record|= 1;
804 }
805
806
807 intervals.resize(interval_count);
808
809 /* Now fix the TYPELIBs for the intervals (enum values)
810 and field names.
811 */
812
813 uint32_t interval_nr= 0;
814
815 for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
816 {
817 message::Table::Field pfield= table.field(fieldnr);
818
819 /* enum typelibs */
820 if (pfield.type() != message::Table::Field::ENUM)
821 continue;
822
823 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
824
825 if (field_options.field_value_size() > Field_enum::max_supported_elements)
826 {
827 my_error(ER_CORRUPT_TABLE_DEFINITION_ENUM, MYF(0), table.name().c_str());
828
829 return true;
830 }
831
832
833 const charset_info_st *charset= get_charset(field_options.has_collation_id() ?
834 field_options.collation_id() : 0);
835
836 if (! charset)
837 charset= default_charset_info;
838
839 TYPELIB *t= (&intervals[interval_nr]);
840
841 t->type_names= (const char**)mem().alloc((field_options.field_value_size() + 1) * sizeof(char*));
842 t->type_lengths= (unsigned int*)mem().alloc((field_options.field_value_size() + 1) * sizeof(unsigned int));
843
844 t->type_names[field_options.field_value_size()]= NULL;
845 t->type_lengths[field_options.field_value_size()]= 0;
846
847 t->count= field_options.field_value_size();
848 t->name= NULL;
849
850 for (int n= 0; n < field_options.field_value_size(); n++)
851 {
852 t->type_names[n]= mem().strdup(field_options.field_value(n));
853
854 /*
855 * Go ask the charset what the length is as for "" length=1
856 * and there's stripping spaces or some other crack going on.
857 */
858 t->type_lengths[n]= charset->cset->lengthsp(charset, t->type_names[n], field_options.field_value(n).length());
859 }
860 interval_nr++;
861 }
862
863
864 /* and read the fields */
865 interval_nr= 0;
866
867 bool use_hash= _field_size >= MAX_FIELDS_BEFORE_HASH;
868
869 unsigned char* null_pos= getDefaultValues();
870 int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
871
872 for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
873 {
874 message::Table::Field pfield= table.field(fieldnr);
875
876 Field::utype unireg_type= Field::NONE;
877
878 if (pfield.has_numeric_options() &&
879 pfield.numeric_options().is_autoincrement())
880 {
881 unireg_type= Field::NEXT_NUMBER;
882 }
883
884 if (pfield.has_options() &&
885 pfield.options().has_default_expression() &&
886 pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
887 {
888 if (pfield.options().has_update_expression() &&
889 pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
890 {
891 unireg_type= Field::TIMESTAMP_DNUN_FIELD;
892 }
893 else if (! pfield.options().has_update_expression())
894 {
895 unireg_type= Field::TIMESTAMP_DN_FIELD;
896 }
897 else
898 {
899 assert(0); // Invalid update value.
900 abort();
901 }
902 }
903 else if (pfield.has_options() &&
904 pfield.options().has_update_expression() &&
905 pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
906 {
907 unireg_type= Field::TIMESTAMP_UN_FIELD;
908 }
909
910 str_ref comment;
911 if (pfield.has_comment())
912 {
913 comment.assign(mem().strdup(pfield.comment()), pfield.comment().size());
914 }
915
916 enum_field_types field_type;
917
918 field_type= proto_field_type_to_drizzle_type(pfield);
919
920 const charset_info_st *charset= &my_charset_bin;
921
922 if (field_type == DRIZZLE_TYPE_BLOB || field_type == DRIZZLE_TYPE_VARCHAR)
923 {
924 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
925
926 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
927
928 if (not charset)
929 charset= default_charset_info;
930 }
931
932 if (field_type == DRIZZLE_TYPE_ENUM)
933 {
934 message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
935
936 charset= get_charset(field_options.has_collation_id() ? field_options.collation_id() : 0);
937
938 if (not charset)
939 charset= default_charset_info;
940 }
941
942 uint8_t decimals= 0;
943 if (field_type == DRIZZLE_TYPE_DECIMAL || field_type == DRIZZLE_TYPE_DOUBLE)
944 {
945 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
946
947 if (not pfield.has_numeric_options() || ! fo.has_scale())
948 {
949 /*
950 We don't write the default to table proto so
951 if no decimals specified for DOUBLE, we use the default.
952 */
953 decimals= NOT_FIXED_DEC;
954 }
955 else
956 {
957 if (fo.scale() > DECIMAL_MAX_SCALE)
958 {
959 local_error= ER_NOT_FORM_FILE;
960
961 return true;
962 }
963 decimals= static_cast<uint8_t>(fo.scale());
964 }
965 }
966
967 Item *default_value= NULL;
968
969 if (pfield.options().has_default_value() ||
970 pfield.options().default_null() ||
971 pfield.options().has_default_bin_value())
972 {
973 default_value= default_value_item(field_type, *charset, pfield.options().default_null(), pfield.options().default_value(), pfield.options().default_bin_value());
974 if (default_value == NULL)
975 {
976 my_error(ER_INVALID_DEFAULT, MYF(0), pfield.name().c_str());
977 return true;
978 }
979 }
980
981
982 uint32_t field_length= 0; //Assignment is for compiler complaint.
983
984 // We set field_length in this loop.
985 switch (field_type)
986 {
987 case DRIZZLE_TYPE_BLOB:
988 case DRIZZLE_TYPE_VARCHAR:
989 {
990 message::Table::Field::StringFieldOptions field_options= pfield.string_options();
991
992 charset= get_charset(field_options.has_collation_id() ?
993 field_options.collation_id() : 0);
994
995 if (! charset)
996 charset= default_charset_info;
997
998 field_length= field_options.length() * charset->mbmaxlen;
999 }
1000 break;
1001 case DRIZZLE_TYPE_DOUBLE:
1002 {
1003 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1004 if (!fo.has_precision() && !fo.has_scale())
1005 {
1006 field_length= DBL_DIG+7;
1007 }
1008 else
1009 {
1010 field_length= fo.precision();
1011 }
1012 if (field_length < decimals &&
1013 decimals != NOT_FIXED_DEC)
1014 {
1015 my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1016 local_error= ER_M_BIGGER_THAN_D;
1017 return true;
1018 }
1019 break;
1020 }
1021 case DRIZZLE_TYPE_DECIMAL:
1022 {
1023 message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1024
1025 field_length= class_decimal_precision_to_length(fo.precision(), fo.scale(),
1026 false);
1027 break;
1028 }
1029 case DRIZZLE_TYPE_DATETIME:
1030 field_length= DateTime::MAX_STRING_LENGTH;
1031 break;
1032 case DRIZZLE_TYPE_DATE:
1033 field_length= Date::MAX_STRING_LENGTH;
1034 break;
1035 case DRIZZLE_TYPE_ENUM:
1036 {
1037 field_length= 0;
1038
1039 message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1040
1041 for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1042 {
1043 if (fo.field_value(valnr).length() > field_length)
1044 {
1045 field_length= charset->cset->numchars(charset,
1046 fo.field_value(valnr).c_str(),
1047 fo.field_value(valnr).c_str()
1048 + fo.field_value(valnr).length())
1049 * charset->mbmaxlen;
1050 }
1051 }
1052 }
1053 break;
1054 case DRIZZLE_TYPE_LONG:
1055 {
1056 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1057 field_length= MAX_INT_WIDTH+sign_len;
1058 }
1059 break;
1060 case DRIZZLE_TYPE_LONGLONG:
1061 {
1062 uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1063 field_length= MAX_BIGINT_WIDTH+sign_len;
1064 }
1065 break;
1066 case DRIZZLE_TYPE_UUID:
1067 field_length= field::Uuid::max_string_length();
1068 break;
1069 case DRIZZLE_TYPE_IPV6:
1070 field_length= field::IPv6::max_string_length();
1071 break;
1072 case DRIZZLE_TYPE_BOOLEAN:
1073 field_length= field::Boolean::max_string_length();
1074 break;
1075 case DRIZZLE_TYPE_MICROTIME:
1076 field_length= field::Microtime::max_string_length();
1077 break;
1078 case DRIZZLE_TYPE_TIMESTAMP:
1079 field_length= field::Epoch::max_string_length();
1080 break;
1081 case DRIZZLE_TYPE_TIME:
1082 field_length= field::Time::max_string_length();
1083 break;
1084 case DRIZZLE_TYPE_NULL:
1085 abort(); // Programming error
1086 }
1087
1088 bool is_not_null= false;
1089
1090 if (not pfield.constraints().is_nullable())
1091 {
1092 is_not_null= true;
1093 }
1094 else if (pfield.constraints().is_notnull())
1095 {
1096 is_not_null= true;
1097 }
1098
1099 Field* f= make_field(pfield,
1100 record + field_offsets[fieldnr] + data_offset,
1101 field_length,
1102 not is_not_null,
1103 null_pos,
1104 null_bit_pos,
1105 decimals,
1106 field_type,
1107 charset,
1108 MTYP_TYPENR(unireg_type),
1109 ((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1110 getTableMessage()->field(fieldnr).name().c_str());
1111
1112 _fields[fieldnr]= f;
1113
1114 // Insert post make_field code here.
1115 switch (field_type)
1116 {
1117 case DRIZZLE_TYPE_BLOB:
1118 case DRIZZLE_TYPE_VARCHAR:
1119 case DRIZZLE_TYPE_DOUBLE:
1120 case DRIZZLE_TYPE_DECIMAL:
1121 case DRIZZLE_TYPE_TIMESTAMP:
1122 case DRIZZLE_TYPE_TIME:
1123 case DRIZZLE_TYPE_DATETIME:
1124 case DRIZZLE_TYPE_MICROTIME:
1125 case DRIZZLE_TYPE_DATE:
1126 case DRIZZLE_TYPE_ENUM:
1127 case DRIZZLE_TYPE_LONG:
1128 case DRIZZLE_TYPE_LONGLONG:
1129 case DRIZZLE_TYPE_NULL:
1130 case DRIZZLE_TYPE_UUID:
1131 case DRIZZLE_TYPE_IPV6:
1132 case DRIZZLE_TYPE_BOOLEAN:
1133 break;
1134 }
1135
1136 // This needs to go, we should be setting the "use" on the field so that
1137 // it does not reference the share/table.
1138 table::Shell temp_table(*this); /* Use this so that BLOB DEFAULT '' works */
1139 temp_table.in_use= &session;
1140
1141 f->init(&temp_table); /* blob default values need table obj */
1142
1143 if (! (f->flags & NOT_NULL_FLAG))
1144 {
1145 *f->null_ptr|= f->null_bit;
1146 if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
1147 null_pos++;
1148 null_count++;
1149 }
1150
1151 if (default_value)
1152 {
1153 enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1154 session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1155 int res= default_value->save_in_field(f, 1);
1156 session.count_cuted_fields= old_count_cuted_fields;
1157 if (res != 0 && res != 3) /* @TODO Huh? */
1158 {
1159 my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1160 local_error= ER_INVALID_DEFAULT;
1161
1162 return true;
1163 }
1164 }
1165 else if (f->real_type() == DRIZZLE_TYPE_ENUM && (f->flags & NOT_NULL_FLAG))
1166 {
1167 f->set_notnull();
1168 f->store((int64_t) 1, true);
1169 }
1170 else
1171 {
1172 f->reset();
1173 }
1174
1175 /* hack to undo f->init() */
1176 f->setTable(NULL);
1177 f->orig_table= NULL;
1178
1179 f->setPosition(fieldnr);
1180 f->comment= comment;
1181 if (not default_value &&
1182 not (f->unireg_check==Field::NEXT_NUMBER) &&
1183 (f->flags & NOT_NULL_FLAG) &&
1184 (not f->is_timestamp()))
1185 {
1186 f->flags|= NO_DEFAULT_VALUE_FLAG;
1187 }
1188
1189 if (f->unireg_check == Field::NEXT_NUMBER)
1190 found_next_number_field= &(_fields[fieldnr]);
1191
1192 if (use_hash) /* supposedly this never fails... but comments lie */
1193 {
1194 const char *local_field_name= _fields[fieldnr]->field_name;
1195 name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1196 }
1197 }
1198
1199 keyinfo= key_info;
1200 for (unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1201 {
1202 key_part= keyinfo->key_part;
1203
1204 for (unsigned int partnr= 0;
1205 partnr < keyinfo->key_parts;
1206 partnr++, key_part++)
1207 {
1208 /*
1209 * Fix up key_part->offset by adding data_offset.
1210 * We really should compute offset as well.
1211 * But at least this way we are a little better.
1212 */
1213 key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1214 }
1215 }
1216
1217 /*
1218 We need to set the unused bits to 1. If the number of bits is a multiple
1219 of 8 there are no unused bits.
1220 */
1221 if (null_count & 7)
1222 *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1223
1224 null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1225
1226 last_null_bit_pos= null_bit_pos;
1227
1228 /* Fix key stuff */
1229 if (key_parts)
1230 {
1231 uint32_t local_primary_key= doesKeyNameExist("PRIMARY");
1232 keyinfo= key_info;
1233 key_part= keyinfo->key_part;
1234
1235 for (uint32_t key= 0; key < keys; key++,keyinfo++)
1236 {
1237 uint32_t usable_parts= 0;
1238
1239 if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1240 {
1241 /*
1242 If the UNIQUE key doesn't have NULL columns and is not a part key
1243 declare this as a primary key.
1244 */
1245 local_primary_key=key;
1246 for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1247 {
1248 uint32_t fieldnr= key_part[i].fieldnr;
1249 if (not fieldnr ||
1250 _fields[fieldnr-1]->null_ptr ||
1251 _fields[fieldnr-1]->key_length() != key_part[i].length)
1252 {
1253 local_primary_key= MAX_KEY; // Can't be used
1254 break;
1255 }
1256 }
1257 }
1258
1259 for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1260 {
1261 Field *local_field;
1262 if (! key_part->fieldnr)
1263 {
1264 return ENOMEM;
1265 }
1266 local_field= key_part->field= _fields[key_part->fieldnr-1];
1267 key_part->type= local_field->key_type();
1268 if (local_field->null_ptr)
1269 {
1270 key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - getDefaultValues());
1271 key_part->null_bit= local_field->null_bit;
1272 key_part->store_length+=HA_KEY_NULL_LENGTH;
1273 keyinfo->flags|=HA_NULL_PART_KEY;
1274 keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1275 keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1276 }
1277 if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1278 local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1279 {
1280 if (local_field->type() == DRIZZLE_TYPE_BLOB)
1281 key_part->key_part_flag|= HA_BLOB_PART;
1282 else
1283 key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1284 keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1285 key_part->store_length+=HA_KEY_BLOB_LENGTH;
1286 keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1287 }
1288 if (i == 0 && key != local_primary_key)
1289 local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1290 (keyinfo->key_parts == 1)) ?
1291 UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1292 if (i == 0)
1293 local_field->key_start.set(key);
1294 if (local_field->key_length() == key_part->length &&
1295 !(local_field->flags & BLOB_FLAG))
1296 {
1297 message::Table::Index::IndexType algo= key_info[key].algorithm;
1298 if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1299 {
1300 keys_for_keyread.set(key);
1301 local_field->part_of_key.set(key);
1302 local_field->part_of_key_not_clustered.set(key);
1303 }
1304 if (db_type()->index_flags(algo) & HA_READ_ORDER)
1305 local_field->part_of_sortkey.set(key);
1306 }
1307 if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1308 usable_parts == i)
1309 usable_parts++; // For FILESORT
1310 local_field->flags|= PART_KEY_FLAG;
1311 if (key == local_primary_key)
1312 {
1313 local_field->flags|= PRI_KEY_FLAG;
1314 /*
1315 If this field is part of the primary key and all keys contains
1316 the primary key, then we can use any key to find this column
1317 */
1318 if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1319 {
1320 local_field->part_of_key= keys_in_use;
1321 if (local_field->part_of_sortkey.test(key))
1322 local_field->part_of_sortkey= keys_in_use;
1323 }
1324 }
1325 if (local_field->key_length() != key_part->length)
1326 {
1327 key_part->key_part_flag|= HA_PART_KEY_SEG;
1328 }
1329 }
1330 keyinfo->usable_key_parts= usable_parts; // Filesort
1331
1332 set_if_bigger(max_key_length,keyinfo->key_length+
1333 keyinfo->key_parts);
1334 total_key_length+= keyinfo->key_length;
1335
1336 if (keyinfo->flags & HA_NOSAME)
1337 {
1338 set_if_bigger(max_unique_length,keyinfo->key_length);
1339 }
1340 }
1341 if (local_primary_key < MAX_KEY &&
1342 (keys_in_use.test(local_primary_key)))
1343 {
1344 primary_key= local_primary_key;
1345 /*
1346 If we are using an integer as the primary key then allow the user to
1347 refer to it as '_rowid'
1348 */
1349 if (key_info[local_primary_key].key_parts == 1)
1350 {
1351 Field *local_field= key_info[local_primary_key].key_part[0].field;
1352 if (local_field && local_field->result_type() == INT_RESULT)
1353 {
1354 /* note that fieldnr here (and rowid_field_offset) starts from 1 */
1355 rowid_field_offset= (key_info[local_primary_key].key_part[0].
1356 fieldnr);
1357 }
1358 }
1359 }
1360 }
1361
1362 if (found_next_number_field)
1363 {
1364 Field *reg_field= *found_next_number_field;
1365 if ((int) (next_number_index= (uint32_t)
1366 find_ref_key(key_info, keys,
1367 getDefaultValues(), reg_field,
1368 &next_number_key_offset,
1369 &next_number_keypart)) < 0)
1370 {
1371 /* Wrong field definition */
1372 local_error= ER_NOT_FORM_FILE;
1373
1374 return true;
1375 }
1376 else
1377 {
1378 reg_field->flags |= AUTO_INCREMENT_FLAG;
1379 }
1380 }
1381
1382 if (blob_fields)
1383 {
1384 /* Store offsets to blob fields to find them fast */
1385 blob_field.resize(blob_fields);
1386 uint32_t *save= &blob_field[0];
1387 uint32_t k= 0;
1388 for (Fields::iterator iter= _fields.begin(); iter != _fields.end()-1; iter++, k++)
1389 {
1390 if ((*iter)->flags & BLOB_FLAG)
1391 (*save++)= k;
1392 }
1393 }
1394
1395 all_set.clear();
1396 all_set.resize(_field_size);
1397 all_set.set();
1398
1399 return local_error != EE_OK;
1400}
1401
1402/*
1403 Read table definition from a binary / text based .frm cursor
1404
1405 SYNOPSIS
1406 open_table_def()
1407 session Thread Cursor
1408 share Fill this with table definition
1409
1410 NOTES
1411 This function is called when the table definition is not cached in
1412 definition::Cache::getCache()
1413 The data is returned in 'share', which is alloced by
1414 alloc_table_share().. The code assumes that share is initialized.
1415
1416 RETURN VALUES
1417 0 ok
1418 1 Error (see open_table_error)
1419 2 Error (see open_table_error)
1420 3 Wrong data in .frm cursor
1421 4 Error (see open_table_error)
1422 5 Error (see open_table_error: charset unavailable)
1423 6 Unknown .frm version
1424*/
1425
1426int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1427{
1428 drizzled::error_t local_error= EE_OK;
1429
1430 message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1431
1432 if (table and table->IsInitialized())
1433 {
1434 if (parse_table_proto(session, *table))
1435 {
1436 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1437 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1438 }
1439 else
1440 {
1441 setTableCategory(TABLE_CATEGORY_USER);
1442 local_error= EE_OK;
1443 }
1444 }
1445 else if (table and not table->IsInitialized())
1446 {
1447 local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1448 my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1449 }
1450 else
1451 {
1452 local_error= ER_TABLE_UNKNOWN;
1453 my_error(ER_TABLE_UNKNOWN, identifier);
1454 }
1455
1456 return static_cast<int>(local_error);
1457}
1458
1459
1460/*
1461 Open a table based on a TableShare
1462
1463 SYNOPSIS
1464 open_table_from_share()
1465 session Thread Cursor
1466 share Table definition
1467 alias Alias for table
1468 db_stat open flags (for example HA_OPEN_KEYFILE|
1469 HA_OPEN_RNDFILE..) can be 0 (example in
1470 ha_example_table)
1471 ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1472 outparam result table
1473
1474 RETURN VALUES
1475 0 ok
1476 1 Error (see open_table_error)
1477 2 Error (see open_table_error)
1478 3 Wrong data in .frm cursor
1479 4 Error (see open_table_error)
1480 5 Error (see open_table_error: charset unavailable)
1481 7 Table definition has changed in engine
1482*/
1483int TableShare::open_table_from_share(Session *session,
1484 const identifier::Table &identifier,
1485 const char *alias,
1486 uint32_t db_stat, uint32_t ha_open_flags,
1487 Table &outparam)
1488{
1489 bool error_reported= false;
1490 int ret= open_table_from_share_inner(session, alias, db_stat, outparam);
1491
1492 if (not ret)
1493 ret= open_table_cursor_inner(identifier, db_stat, ha_open_flags, outparam, error_reported);
1494
1495 if (not ret)
1496 return ret;
1497
1498 if (not error_reported)
1499 open_table_error(ret, errno, 0);
1500
1501 boost::checked_delete(outparam.cursor);
1502 outparam.cursor= 0; // For easier error checking
1503 outparam.db_stat= 0;
1504 outparam.mem().free_root(MYF(0)); // Safe to call on zeroed root
1505 outparam.clearAlias();
1506
1507 return ret;
1508}
1509
1510int TableShare::open_table_from_share_inner(Session *session, const char *alias, uint32_t db_stat, Table &outparam)
1511{
1512 int local_error= 1;
1513 outparam.resetTable(session, this, db_stat);
1514
1515 outparam.setAlias(alias);
1516
1517 /* Allocate Cursor */
1518 if (not (outparam.cursor= db_type()->getCursor(outparam)))
1519 return local_error;
1520
1521 local_error= 4;
1522 uint32_t records= 0;
1523 if (db_stat & HA_OPEN_KEYFILE)
1524 records=1;
1525
1526 records++;
1527
1528 unsigned char* record= outparam.alloc(rec_buff_length * records);
1529
1530 if (records == 0)
1531 {
1532 /* We are probably in hard repair, and the buffers should not be used */
1533 outparam.record[0]= outparam.record[1]= getDefaultValues();
1534 }
1535 else
1536 {
1537 outparam.record[0]= record;
1538 if (records > 1)
1539 outparam.record[1]= record+ rec_buff_length;
1540 else
1541 outparam.record[1]= outparam.getInsertRecord(); // Safety
1542 }
1543
1544#ifdef HAVE_VALGRIND
1545 /*
1546 We need this because when we read var-length rows, we are not updating
1547 bytes after end of varchar
1548 */
1549 if (records > 1)
1550 {
1551 memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1552 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1553 if (records > 2)
1554 memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1555 }
1556#endif
1557 if (records > 1)
1558 {
1559 memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1560 }
1561
1562 Field** field_ptr = new (outparam.mem()) Field*[_field_size + 1];
1563
1564 outparam.setFields(field_ptr);
1565
1566 record= outparam.getInsertRecord()-1; /* Fieldstart = 1 */
1567
1568 outparam.null_flags= (unsigned char*) record+1;
1569
1570 /* Setup copy of fields from share, but use the right alias and record */
1571 for (uint32_t i= 0 ; i < _field_size; i++, field_ptr++)
1572 {
1573 if (!((*field_ptr)= _fields[i]->clone(&outparam.mem(), &outparam)))
1574 return local_error;
1575 }
1576 *field_ptr= 0; // End marker
1577
1578 if (found_next_number_field)
1579 outparam.found_next_number_field=
1580 outparam.getField(positionFields(found_next_number_field));
1581 if (timestamp_field)
1582 outparam.timestamp_field= (field::Epoch*) outparam.getField(timestamp_field->position());
1583
1584 /* Fix key->name and key_part->field */
1585 if (key_parts)
1586 {
1587 uint32_t n_length= keys * sizeof(KeyInfo) + key_parts * sizeof(KeyPartInfo);
1588 KeyInfo* local_key_info= (KeyInfo*) outparam.alloc(n_length);
1589 outparam.key_info= local_key_info;
1590 KeyPartInfo* key_part= reinterpret_cast<KeyPartInfo*>(local_key_info+keys);
1591
1592 memcpy(local_key_info, key_info, sizeof(*local_key_info)*keys);
1593 memcpy(key_part, key_info[0].key_part, sizeof(*key_part) * key_parts);
1594
1595 for (KeyInfo* key_info_end= local_key_info + keys; local_key_info < key_info_end; local_key_info++)
1596 {
1597 local_key_info->table= &outparam;
1598 local_key_info->key_part= key_part;
1599
1600 for (KeyPartInfo* key_part_end= key_part+ local_key_info->key_parts; key_part < key_part_end; key_part++)
1601 {
1602 Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
1603
1604 if (local_field->key_length() != key_part->length && not (local_field->flags & BLOB_FLAG))
1605 {
1606 /*
1607 We are using only a prefix of the column as a key:
1608 Create a new field for the key part that matches the index
1609 */
1610 local_field= key_part->field= local_field->new_field(&outparam.mem(), &outparam, 0);
1611 local_field->field_length= key_part->length;
1612 }
1613 }
1614 }
1615 }
1616
1617 /* Allocate bitmaps */
1618
1619 outparam.def_read_set.resize(_field_size);
1620 outparam.def_write_set.resize(_field_size);
1621 outparam.tmp_set.resize(_field_size);
1622 outparam.default_column_bitmaps();
1623
1624 return 0;
1625}
1626
1627int TableShare::open_table_cursor_inner(const identifier::Table &identifier,
1628 uint32_t db_stat, uint32_t ha_open_flags,
1629 Table &outparam,
1630 bool &error_reported)
1631{
1632 /* The table struct is now initialized; Open the table */
1633 int local_error= 2;
1634 if (db_stat)
1635 {
1636 assert(!(db_stat & HA_WAIT_IF_LOCKED));
1637 int ha_err;
1638
1639 if ((ha_err= (outparam.cursor->ha_open(identifier,
1640 (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1641 (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1642 {
1643 switch (ha_err)
1644 {
1645 case HA_ERR_NO_SUCH_TABLE:
1646 /*
1647 The table did not exists in storage engine, use same error message
1648 as if the .frm cursor didn't exist
1649 */
1650 local_error= 1;
1651 errno= ENOENT;
1652 break;
1653 case EMFILE:
1654 /*
1655 Too many files opened, use same error message as if the .frm
1656 cursor can't open
1657 */
1658 local_error= 1;
1659 errno= EMFILE;
1660 break;
1661 default:
1662 outparam.print_error(ha_err, MYF(0));
1663 error_reported= true;
1664 if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1665 local_error= 7;
1666 break;
1667 }
1668 return local_error;
1669 }
1670 }
1671
1672 return 0;
1673}
1674
1675/* error message when opening a form cursor */
1676void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1677{
1678 char buff[FN_REFLEN];
1679 myf errortype= ME_ERROR+ME_WAITTANG;
1680
1681 switch (pass_error) {
1682 case 7:
1683 case 1:
1684 if (db_errno == ENOENT)
1685 {
1686 my_error(ER_TABLE_UNKNOWN, *table_identifier);
1687 }
1688 else
1689 {
1690 snprintf(buff, sizeof(buff), "%s",normalized_path.data());
1691 my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND, errortype, buff, db_errno);
1692 }
1693 break;
1694 case 2:
1695 {
1696 drizzled::error_t err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE;
1697 my_error(err_no, errortype, normalized_path.data(), db_errno);
1698 break;
1699 }
1700 case 5:
1701 {
1702 const char *csname= get_charset_name((uint32_t) pass_errarg);
1703 char tmp[10];
1704 if (!csname || csname[0] =='?')
1705 {
1706 snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
1707 csname= tmp;
1708 }
1709 my_printf_error(ER_UNKNOWN_COLLATION, _("Unknown collation '%s' in table '%-.64s' definition"), MYF(0), csname, table_identifier->getTableName().c_str());
1710 break;
1711 }
1712 case 6:
1713 snprintf(buff, sizeof(buff), "%s", normalized_path.data());
1714 my_printf_error(ER_NOT_FORM_FILE, _("Table '%-.64s' was created with a different version of Drizzle and cannot be read"), MYF(0), buff);
1715 break;
1716 case 8:
1717 break;
1718 default: /* Better wrong error than none */
1719 case 4:
1720 snprintf(buff, sizeof(buff), "%s", normalized_path.data());
1721 my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
1722 break;
1723 }
1724 return;
1725} /* open_table_error */
1726
1727Field *TableShare::make_field(const message::Table::Field &pfield,
1728 unsigned char *ptr,
1729 uint32_t field_length,
1730 bool is_nullable,
1731 unsigned char *null_pos,
1732 unsigned char null_bit,
1733 uint8_t decimals,
1734 enum_field_types field_type,
1735 const charset_info_st * field_charset,
1736 Field::utype unireg_check,
1737 TYPELIB *interval,
1738 const char *field_name)
1739{
1740 return make_field(pfield,
1741 ptr,
1742 field_length,
1743 is_nullable,
1744 null_pos,
1745 null_bit,
1746 decimals,
1747 field_type,
1748 field_charset,
1749 unireg_check,
1750 interval,
1751 field_name,
1752 pfield.constraints().is_unsigned());
1753}
1754
1755Field *TableShare::make_field(const message::Table::Field &,
1756 unsigned char *ptr,
1757 uint32_t field_length,
1758 bool is_nullable,
1759 unsigned char *null_pos,
1760 unsigned char null_bit,
1761 uint8_t decimals,
1762 enum_field_types field_type,
1763 const charset_info_st * field_charset,
1764 Field::utype unireg_check,
1765 TYPELIB *interval,
1766 const char *field_name,
1767 bool is_unsigned)
1768{
1769 if (! is_nullable)
1770 {
1771 null_pos=0;
1772 null_bit=0;
1773 }
1774 else
1775 {
1776 null_bit= ((unsigned char) 1) << null_bit;
1777 }
1778
1779 switch (field_type)
1780 {
1781 case DRIZZLE_TYPE_ENUM:
1782 return new (&mem_root) Field_enum(ptr,
1783 field_length,
1784 null_pos,
1785 null_bit,
1786 field_name,
1787 interval,
1788 field_charset);
1789 case DRIZZLE_TYPE_VARCHAR:
1790 setVariableWidth();
1791 return new (&mem_root) Field_varstring(ptr,field_length,
1792 ha_varchar_packlength(field_length),
1793 null_pos,null_bit,
1794 field_name,
1795 field_charset);
1796 case DRIZZLE_TYPE_BLOB:
1797 return new (&mem_root) Field_blob(ptr,
1798 null_pos,
1799 null_bit,
1800 field_name,
1801 this,
1802 field_charset);
1803 case DRIZZLE_TYPE_DECIMAL:
1804 return new (&mem_root) Field_decimal(ptr,
1805 field_length,
1806 null_pos,
1807 null_bit,
1808 unireg_check,
1809 field_name,
1810 decimals);
1811 case DRIZZLE_TYPE_DOUBLE:
1812 return new (&mem_root) Field_double(ptr,
1813 field_length,
1814 null_pos,
1815 null_bit,
1816 unireg_check,
1817 field_name,
1818 decimals,
1819 false,
1820 false /* is_unsigned */);
1821 case DRIZZLE_TYPE_UUID:
1822 return new (&mem_root) field::Uuid(ptr,
1823 field_length,
1824 null_pos,
1825 null_bit,
1826 field_name);
1827 case DRIZZLE_TYPE_IPV6:
1828 return new (&mem_root) field::IPv6(ptr,
1829 field_length,
1830 null_pos,
1831 null_bit,
1832 field_name);
1833 case DRIZZLE_TYPE_BOOLEAN:
1834 return new (&mem_root) field::Boolean(ptr,
1835 field_length,
1836 null_pos,
1837 null_bit,
1838 field_name,
1839 is_unsigned);
1840 case DRIZZLE_TYPE_LONG:
1841 return new (&mem_root) field::Int32(ptr,
1842 field_length,
1843 null_pos,
1844 null_bit,
1845 unireg_check,
1846 field_name);
1847 case DRIZZLE_TYPE_LONGLONG:
1848 {
1849 if (is_unsigned)
1850 {
1851 return new (&mem_root) field::Size(ptr,
1852 field_length,
1853 null_pos,
1854 null_bit,
1855 unireg_check,
1856 field_name);
1857 }
1858
1859 return new (&mem_root) field::Int64(ptr,
1860 field_length,
1861 null_pos,
1862 null_bit,
1863 unireg_check,
1864 field_name);
1865 }
1866 case DRIZZLE_TYPE_MICROTIME:
1867 return new (&mem_root) field::Microtime(ptr,
1868 null_pos,
1869 null_bit,
1870 unireg_check,
1871 field_name,
1872 this);
1873 case DRIZZLE_TYPE_TIMESTAMP:
1874 return new (&mem_root) field::Epoch(ptr,
1875 null_pos,
1876 null_bit,
1877 unireg_check,
1878 field_name,
1879 this);
1880 case DRIZZLE_TYPE_TIME:
1881 return new (&mem_root) field::Time(ptr, field_length, null_pos, null_bit, field_name);
1882 case DRIZZLE_TYPE_DATE:
1883 return new (&mem_root) Field_date(ptr, null_pos, null_bit, field_name);
1884 case DRIZZLE_TYPE_DATETIME:
1885 return new (&mem_root) Field_datetime(ptr, null_pos, null_bit, field_name);
1886 case DRIZZLE_TYPE_NULL:
1887 return new (&mem_root) Field_null(ptr, field_length, field_name);
1888 }
1889 assert(false);
1890 abort();
1891}
1892
1893void TableShare::refreshVersion()
1894{
1895 version= g_refresh_version;
1896}
1897
1898
1899} /* namespace drizzled */
static const int MAX_STRING_LENGTH
Definition temporal.h:608
static const int MAX_STRING_LENGTH
Definition temporal.h:310
enum_table_category table_category
bool fieldInPrimaryKey(Field *field) const
Definition base.cc:202
TODO: Rename this file - func.h is stupid.
@ TABLE_UNKNOWN_CATEGORY
Definition enum.h:98
@ TABLE_CATEGORY_USER
Definition enum.h:121
@ TABLE_CATEGORY_TEMPORARY
Definition enum.h:111
STL namespace.