17
17
/* Some general useful functions */
19
#include <drizzled/server_includes.h>
28
20
#include <drizzled/error.h>
29
21
#include <drizzled/gettext.h>
31
#include "drizzled/plugin/transactional_storage_engine.h"
32
#include "drizzled/plugin/authorization.h"
23
#include <drizzled/sj_tmp_table.h>
33
24
#include <drizzled/nested_join.h>
25
#include <drizzled/data_home.h>
34
26
#include <drizzled/sql_parse.h>
35
27
#include <drizzled/item/sum.h>
36
28
#include <drizzled/table_list.h>
37
29
#include <drizzled/session.h>
38
30
#include <drizzled/sql_base.h>
39
#include <drizzled/sql_select.h>
40
31
#include <drizzled/field/blob.h>
41
32
#include <drizzled/field/varstring.h>
42
33
#include <drizzled/field/double.h>
43
37
#include <drizzled/unireg.h>
44
38
#include <drizzled/message/table.pb.h>
45
#include "drizzled/sql_table.h"
46
#include "drizzled/charset.h"
47
#include "drizzled/internal/m_string.h"
48
#include "plugin/myisam/myisam.h"
50
40
#include <drizzled/item/string.h>
51
41
#include <drizzled/item/int.h>
52
42
#include <drizzled/item/decimal.h>
53
43
#include <drizzled/item/float.h>
54
44
#include <drizzled/item/null.h>
55
#include <drizzled/temporal.h>
57
#include "drizzled/table_proto.h"
59
47
using namespace std;
64
extern pid_t current_pid;
65
extern plugin::StorageEngine *heap_engine;
66
extern plugin::StorageEngine *myisam_engine;
68
/* Functions defined in this cursor */
49
/* Functions defined in this file */
70
51
void open_table_error(TableShare *share, int error, int db_errno,
71
52
myf errortype, int errarg);
80
61
return (unsigned char*) (*buff)->field_name;
66
Returns pointer to '.frm' extension of the file name.
73
Checks file name part starting with the rightmost '.' character,
74
and returns it if it is equal to '.dfe'.
77
It is a good idea to get rid of this function modifying the code
78
to garantee that the functions presently calling fn_rext() always
79
get arguments in the same format: either with '.frm' or without '.frm'.
82
Pointer to the '.frm' extension. If there is no extension,
83
or extension is not '.frm', pointer at the end of file name.
86
char *fn_rext(char *name)
88
char *res= strrchr(name, '.');
89
if (res && !strcmp(res, ".dfe"))
91
return name + strlen(name);
94
TABLE_CATEGORY get_table_category(const LEX_STRING *db, const LEX_STRING *name)
99
if ((db->length == INFORMATION_SCHEMA_NAME.length()) &&
100
(my_strcasecmp(system_charset_info,
101
INFORMATION_SCHEMA_NAME.c_str(),
104
return TABLE_CATEGORY_INFORMATION;
107
return TABLE_CATEGORY_USER;
84
112
Allocate a setup TableShare structure
97
125
TableShare *alloc_table_share(TableList *table_list, char *key,
98
126
uint32_t key_length)
100
memory::Root mem_root;
101
129
TableShare *share;
102
130
char *key_buff, *path_buff;
105
build_table_filename(path, table_list->db, table_list->table_name, false);
107
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
131
char path[FN_REFLEN];
132
uint32_t path_length;
134
path_length= build_table_filename(path, sizeof(path) - 1,
136
table_list->table_name, false);
137
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
108
138
if (multi_alloc_root(&mem_root,
109
139
&share, sizeof(*share),
110
140
&key_buff, key_length,
111
&path_buff, path.length() + 1,
141
&path_buff, path_length + 1,
114
144
memset(share, 0, sizeof(*share));
116
146
share->set_table_cache_key(key_buff, key, key_length);
118
share->path.str= path_buff,
119
share->path.length= path.length();
120
strcpy(share->path.str, path.c_str());
148
share->path.str= path_buff;
149
share->path.length= path_length;
150
strcpy(share->path.str, path);
121
151
share->normalized_path.str= share->path.str;
122
share->normalized_path.length= path.length();
152
share->normalized_path.length= path_length;
124
154
share->version= refresh_version;
134
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
164
enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
136
166
enum_field_types field_type;
138
168
switch(proto_field_type)
140
case message::Table::Field::INTEGER:
170
case drizzled::message::Table::Field::TINYINT:
171
field_type= DRIZZLE_TYPE_TINY;
173
case drizzled::message::Table::Field::INTEGER:
141
174
field_type= DRIZZLE_TYPE_LONG;
143
case message::Table::Field::DOUBLE:
176
case drizzled::message::Table::Field::DOUBLE:
144
177
field_type= DRIZZLE_TYPE_DOUBLE;
146
case message::Table::Field::TIMESTAMP:
179
case drizzled::message::Table::Field::TIMESTAMP:
147
180
field_type= DRIZZLE_TYPE_TIMESTAMP;
149
case message::Table::Field::BIGINT:
182
case drizzled::message::Table::Field::BIGINT:
150
183
field_type= DRIZZLE_TYPE_LONGLONG;
152
case message::Table::Field::DATETIME:
185
case drizzled::message::Table::Field::DATETIME:
153
186
field_type= DRIZZLE_TYPE_DATETIME;
155
case message::Table::Field::DATE:
188
case drizzled::message::Table::Field::DATE:
156
189
field_type= DRIZZLE_TYPE_DATE;
158
case message::Table::Field::VARCHAR:
191
case drizzled::message::Table::Field::VARCHAR:
159
192
field_type= DRIZZLE_TYPE_VARCHAR;
161
case message::Table::Field::DECIMAL:
162
field_type= DRIZZLE_TYPE_DECIMAL;
194
case drizzled::message::Table::Field::DECIMAL:
195
field_type= DRIZZLE_TYPE_NEWDECIMAL;
164
case message::Table::Field::ENUM:
197
case drizzled::message::Table::Field::ENUM:
165
198
field_type= DRIZZLE_TYPE_ENUM;
167
case message::Table::Field::BLOB:
200
case drizzled::message::Table::Field::BLOB:
168
201
field_type= DRIZZLE_TYPE_BLOB;
171
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
204
field_type= DRIZZLE_TYPE_TINY; /* Set value to kill GCC warning */
175
208
return field_type;
178
static Item *default_value_item(enum_field_types field_type,
179
const CHARSET_INFO *charset,
180
bool default_null, const string *default_value,
181
const string *default_bin_value)
211
Item * default_value_item(enum_field_types field_type,
212
const CHARSET_INFO *charset,
213
bool default_null, const string *default_value,
214
const string *default_bin_value)
183
216
Item *default_item= NULL;
353
413
if (indx.has_options())
355
message::Table::Index::IndexOptions indx_options= indx.options();
415
drizzled::message::Table::Index::IndexOptions indx_options= indx.options();
356
416
if (indx_options.pack_key())
357
keyinfo->flags|= HA_PACK_KEY;
417
keyinfo->flags|= HA_PACK_KEY;
359
419
if (indx_options.var_length_key())
360
keyinfo->flags|= HA_VAR_LENGTH_PART;
420
keyinfo->flags|= HA_VAR_LENGTH_PART;
362
422
if (indx_options.null_part_key())
363
keyinfo->flags|= HA_NULL_PART_KEY;
423
keyinfo->flags|= HA_NULL_PART_KEY;
365
425
if (indx_options.binary_pack_key())
366
keyinfo->flags|= HA_BINARY_PACK_KEY;
426
keyinfo->flags|= HA_BINARY_PACK_KEY;
368
428
if (indx_options.has_partial_segments())
369
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
429
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
371
431
if (indx_options.auto_generated_key())
372
keyinfo->flags|= HA_GENERATED_KEY;
432
keyinfo->flags|= HA_GENERATED_KEY;
374
434
if (indx_options.has_key_block_size())
376
keyinfo->flags|= HA_USES_BLOCK_SIZE;
377
keyinfo->block_size= indx_options.key_block_size();
436
keyinfo->flags|= HA_USES_BLOCK_SIZE;
437
keyinfo->block_size= indx_options.key_block_size();
381
keyinfo->block_size= 0;
441
keyinfo->block_size= 0;
387
case message::Table::Index::UNKNOWN_INDEX:
448
case drizzled::message::Table::Index::UNKNOWN_INDEX:
388
449
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
390
case message::Table::Index::BTREE:
451
case drizzled::message::Table::Index::BTREE:
391
452
keyinfo->algorithm= HA_KEY_ALG_BTREE;
393
case message::Table::Index::HASH:
454
case drizzled::message::Table::Index::RTREE:
455
keyinfo->algorithm= HA_KEY_ALG_RTREE;
457
case drizzled::message::Table::Index::HASH:
394
458
keyinfo->algorithm= HA_KEY_ALG_HASH;
460
case drizzled::message::Table::Index::FULLTEXT:
461
keyinfo->algorithm= HA_KEY_ALG_FULLTEXT;
398
464
/* TODO: suitable warning ? */
498
586
case DRIZZLE_TYPE_BLOB:
499
587
case DRIZZLE_TYPE_VARCHAR:
501
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
503
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
504
field_options.collation_id() : 0);
507
cs= default_charset_info;
509
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
510
field_options.length() * cs->mbmaxlen);
589
drizzled::message::Table::Field::StringFieldOptions field_options=
590
pfield.string_options();
592
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id()?
593
field_options.collation_id() : 0);
596
cs= default_charset_info;
598
field_pack_length[fieldnr]=
599
calc_pack_length(drizzle_field_type,
600
field_options.length() * cs->mbmaxlen);
513
604
case DRIZZLE_TYPE_ENUM:
515
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
517
field_pack_length[fieldnr]=
518
get_enum_pack_length(field_options.field_value_size());
521
interval_parts+= field_options.field_value_size();
606
drizzled::message::Table::Field::SetFieldOptions field_options=
607
pfield.set_options();
609
field_pack_length[fieldnr]=
610
get_enum_pack_length(field_options.field_value_size());
613
interval_parts+= field_options.field_value_size();
524
case DRIZZLE_TYPE_DECIMAL:
616
case DRIZZLE_TYPE_NEWDECIMAL:
526
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
618
drizzled::message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
528
field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
620
field_pack_length[fieldnr]=
621
my_decimal_get_binary_size(fo.precision(), fo.scale());
602
696
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
604
message::Table::Field pfield= table.field(fieldnr);
698
drizzled::message::Table::Field pfield= table.field(fieldnr);
606
700
/* field names */
607
701
share->fieldnames.type_names[fieldnr]= strmake_root(&share->mem_root,
608
pfield.name().c_str(),
609
pfield.name().length());
702
pfield.name().c_str(),
703
pfield.name().length());
611
705
share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
613
707
/* enum typelibs */
614
if (pfield.type() != message::Table::Field::ENUM)
708
if (pfield.type() != drizzled::message::Table::Field::ENUM)
617
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
619
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
620
field_options.collation_id() : 0);
711
drizzled::message::Table::Field::SetFieldOptions field_options=
712
pfield.set_options();
714
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id()?
715
field_options.collation_id() : 0);
623
718
charset= default_charset_info;
625
720
TYPELIB *t= &(share->intervals[interval_nr]);
627
722
t->type_names= (const char**)alloc_root(&share->mem_root,
628
(field_options.field_value_size() + 1) * sizeof(char*));
723
(field_options.field_value_size()+1)*sizeof(char*));
630
725
t->type_lengths= (unsigned int*) alloc_root(&share->mem_root,
631
(field_options.field_value_size() + 1) * sizeof(unsigned int));
726
(field_options.field_value_size()+1)*sizeof(unsigned int));
633
728
t->type_names[field_options.field_value_size()]= NULL;
634
729
t->type_lengths[field_options.field_value_size()]= 0;
698
787
Field::utype unireg_type= Field::NONE;
700
if (pfield.has_numeric_options() &&
701
pfield.numeric_options().is_autoincrement())
789
if (pfield.has_numeric_options()
790
&& pfield.numeric_options().is_autoincrement())
703
792
unireg_type= Field::NEXT_NUMBER;
706
if (pfield.has_options() &&
707
pfield.options().has_default_value() &&
708
pfield.options().default_value().compare("NOW()") == 0)
795
if (pfield.has_options()
796
&& pfield.options().has_default_value()
797
&& pfield.options().default_value().compare("NOW()") == 0)
710
if (pfield.options().has_update_value() &&
711
pfield.options().update_value().compare("NOW()") == 0)
799
if (pfield.options().has_update_value()
800
&& pfield.options().update_value().compare("NOW()") == 0)
713
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
802
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
715
else if (! pfield.options().has_update_value())
804
else if (!pfield.options().has_update_value())
717
unireg_type= Field::TIMESTAMP_DN_FIELD;
806
unireg_type= Field::TIMESTAMP_DN_FIELD;
720
assert(1); // Invalid update value.
809
assert(1); // Invalid update value.
722
else if (pfield.has_options() &&
723
pfield.options().has_update_value() &&
724
pfield.options().update_value().compare("NOW()") == 0)
811
else if (pfield.has_options()
812
&& pfield.options().has_update_value()
813
&& pfield.options().update_value().compare("NOW()") == 0)
726
815
unireg_type= Field::TIMESTAMP_UN_FIELD;
748
837
const CHARSET_INFO *charset= &my_charset_bin;
750
if (field_type == DRIZZLE_TYPE_BLOB ||
751
field_type == DRIZZLE_TYPE_VARCHAR)
753
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
755
charset= get_charset(field_options.has_collation_id() ?
756
field_options.collation_id() : 0);
759
charset= default_charset_info;
762
if (field_type == DRIZZLE_TYPE_ENUM)
764
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
766
charset= get_charset(field_options.has_collation_id()?
767
field_options.collation_id() : 0);
770
charset= default_charset_info;
774
if (field_type == DRIZZLE_TYPE_DECIMAL
775
|| field_type == DRIZZLE_TYPE_DOUBLE)
777
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
779
if (! pfield.has_numeric_options() || ! fo.has_scale())
782
We don't write the default to table proto so
783
if no decimals specified for DOUBLE, we use the default.
785
decimals= NOT_FIXED_DEC;
789
if (fo.scale() > DECIMAL_MAX_SCALE)
794
decimals= static_cast<uint8_t>(fo.scale());
839
if (field_type==DRIZZLE_TYPE_BLOB
840
|| field_type==DRIZZLE_TYPE_VARCHAR)
842
drizzled::message::Table::Field::StringFieldOptions field_options=
843
pfield.string_options();
845
charset= get_charset(field_options.has_collation_id()?
846
field_options.collation_id() : 0);
849
charset= default_charset_info;
853
if (field_type==DRIZZLE_TYPE_ENUM)
855
drizzled::message::Table::Field::SetFieldOptions field_options=
856
pfield.set_options();
858
charset= get_charset(field_options.has_collation_id()?
859
field_options.collation_id() : 0);
862
charset= default_charset_info;
798
866
Item *default_value= NULL;
800
if (pfield.options().has_default_value() ||
801
pfield.options().has_default_null() ||
802
pfield.options().has_default_bin_value())
868
if (pfield.options().has_default_value()
869
|| pfield.options().has_default_null()
870
|| pfield.options().has_default_bin_value())
804
872
default_value= default_value_item(field_type,
806
pfield.options().default_null(),
807
&pfield.options().default_value(),
808
&pfield.options().default_bin_value());
874
pfield.options().default_null(),
875
&pfield.options().default_value(),
876
&pfield.options().default_bin_value());
879
uint32_t pack_flag= pfield.pack_flag(); /* TODO: MUST DIE */
812
881
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
813
882
memset(&temp_table, 0, sizeof(temp_table));
814
883
temp_table.s= share;
815
temp_table.in_use= &session;
816
temp_table.s->db_low_byte_first= true; //Cursor->low_byte_first();
884
temp_table.in_use= session;
885
temp_table.s->db_low_byte_first= 1; //handler->low_byte_first();
817
886
temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
819
uint32_t field_length= 0; //Assignment is for compiler complaint.
823
case DRIZZLE_TYPE_BLOB:
824
case DRIZZLE_TYPE_VARCHAR:
826
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
828
charset= get_charset(field_options.has_collation_id() ?
829
field_options.collation_id() : 0);
832
charset= default_charset_info;
834
field_length= field_options.length() * charset->mbmaxlen;
837
case DRIZZLE_TYPE_DOUBLE:
839
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
840
if (!fo.has_precision() && !fo.has_scale())
842
field_length= DBL_DIG+7;
846
field_length= fo.precision();
848
if (field_length < decimals &&
849
decimals != NOT_FIXED_DEC)
851
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
857
case DRIZZLE_TYPE_DECIMAL:
859
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
861
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
865
case DRIZZLE_TYPE_TIMESTAMP:
866
case DRIZZLE_TYPE_DATETIME:
867
field_length= DateTime::MAX_STRING_LENGTH;
869
case DRIZZLE_TYPE_DATE:
870
field_length= Date::MAX_STRING_LENGTH;
872
case DRIZZLE_TYPE_ENUM:
876
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
878
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
880
if (fo.field_value(valnr).length() > field_length)
882
field_length= charset->cset->numchars(charset,
883
fo.field_value(valnr).c_str(),
884
fo.field_value(valnr).c_str()
885
+ fo.field_value(valnr).length())
891
case DRIZZLE_TYPE_LONG:
893
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
894
field_length= MAX_INT_WIDTH+sign_len;
897
case DRIZZLE_TYPE_LONGLONG:
898
field_length= MAX_BIGINT_WIDTH;
900
case DRIZZLE_TYPE_NULL:
901
abort(); // Programming error
904
Field* f= make_field(share,
906
record + field_offsets[fieldnr] + data_offset,
908
pfield.constraints().is_nullable(),
914
(Field::utype) MTYP_TYPENR(unireg_type),
915
((field_type == DRIZZLE_TYPE_ENUM) ?
916
share->intervals + (interval_nr++)
918
share->fieldnames.type_names[fieldnr]);
888
Field* f= make_field(share, &share->mem_root,
889
record+field_offsets[fieldnr]+data_offset,
890
pfield.options().length(),
896
(Field::utype) MTYP_TYPENR(unireg_type),
897
((field_type==DRIZZLE_TYPE_ENUM)?
898
share->intervals+(interval_nr++)
900
share->fieldnames.type_names[fieldnr]);
920
902
share->field[fieldnr]= f;
922
904
f->init(&temp_table); /* blob default values need table obj */
924
if (! (f->flags & NOT_NULL_FLAG))
906
if (!(f->flags & NOT_NULL_FLAG))
926
908
*f->null_ptr|= f->null_bit;
927
if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
909
if (!(null_bit_pos= (null_bit_pos + 1) & 7))
932
914
if (default_value)
934
enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
935
session.count_cuted_fields= CHECK_FIELD_WARN;
916
enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
917
session->count_cuted_fields= CHECK_FIELD_WARN;
936
918
int res= default_value->save_in_field(f, 1);
937
session.count_cuted_fields= old_count_cuted_fields;
938
if (res != 0 && res != 3) /* @TODO Huh? */
919
session->count_cuted_fields= old_count_cuted_fields;
920
if (res != 0 && res != 3)
940
922
my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
945
927
else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
946
(f->flags & NOT_NULL_FLAG))
928
(f->flags & NOT_NULL_FLAG))
948
930
f->set_notnull();
949
931
f->store((int64_t) 1, true);
1003
981
if (null_count & 7)
1004
982
*(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1006
share->null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
984
share->null_bytes= (null_pos - (unsigned char*) record +
985
(null_bit_pos + 7) / 8);
1008
987
share->last_null_bit_pos= null_bit_pos;
1010
989
free(field_offsets);
1011
field_offsets= NULL;
1012
990
free(field_pack_length);
1013
field_pack_length= NULL;
992
if (!(handler_file= get_new_handler(share, session->mem_root,
1015
996
/* Fix key stuff */
1016
997
if (share->key_parts)
1018
uint32_t primary_key= (uint32_t) (find_type((char*) "PRIMARY",
1019
&share->keynames, 3) - 1); /* @TODO Huh? */
999
uint32_t primary_key=(uint32_t) (find_type((char*) "PRIMARY",
1000
&share->keynames, 3) - 1);
1002
int64_t ha_option= handler_file->ha_table_flags();
1021
1004
keyinfo= share->key_info;
1022
1005
key_part= keyinfo->key_part;
1024
for (uint32_t key= 0; key < share->keys; key++,keyinfo++)
1007
for (uint32_t key= 0 ; key < share->keys ; key++,keyinfo++)
1026
1009
uint32_t usable_parts= 0;
1028
1011
if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1031
If the UNIQUE key doesn't have NULL columns and is not a part key
1032
declare this as a primary key.
1035
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1037
uint32_t fieldnr= key_part[i].fieldnr;
1039
share->field[fieldnr-1]->null_ptr ||
1040
share->field[fieldnr-1]->key_length() != key_part[i].length)
1042
primary_key= MAX_KEY; // Can't be used
1014
If the UNIQUE key doesn't have NULL columns and is not a part key
1015
declare this as a primary key.
1018
for (uint32_t i= 0 ; i < keyinfo->key_parts ;i++)
1020
uint32_t fieldnr= key_part[i].fieldnr;
1022
share->field[fieldnr-1]->null_ptr ||
1023
share->field[fieldnr-1]->key_length() !=
1026
primary_key=MAX_KEY; // Can't be used
1048
1032
for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1051
if (! key_part->fieldnr)
1035
if (!key_part->fieldnr)
1053
abort(); // goto err;
1037
// error= 4; // Wrong file
1038
abort(); // goto err;
1055
1040
field= key_part->field= share->field[key_part->fieldnr-1];
1056
1041
key_part->type= field->key_type();
1122
1106
set_if_bigger(share->max_key_length,keyinfo->key_length+
1123
1107
keyinfo->key_parts);
1124
1108
share->total_key_length+= keyinfo->key_length;
1126
if (keyinfo->flags & HA_NOSAME)
1110
MERGE tables do not have unique indexes. But every key could be
1111
an unique index on the underlying MyISAM table. (Bug #10400)
1113
if ((keyinfo->flags & HA_NOSAME) ||
1114
(ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
1128
1115
set_if_bigger(share->max_unique_length,keyinfo->key_length);
1131
1117
if (primary_key < MAX_KEY &&
1132
(share->keys_in_use.test(primary_key)))
1118
(share->keys_in_use.test(primary_key)))
1134
1120
share->primary_key= primary_key;
1136
If we are using an integer as the primary key then allow the user to
1137
refer to it as '_rowid'
1122
If we are using an integer as the primary key then allow the user to
1123
refer to it as '_rowid'
1139
1125
if (share->key_info[primary_key].key_parts == 1)
1141
Field *field= share->key_info[primary_key].key_part[0].field;
1142
if (field && field->result_type() == INT_RESULT)
1127
Field *field= share->key_info[primary_key].key_part[0].field;
1128
if (field && field->result_type() == INT_RESULT)
1144
1130
/* note that fieldnr here (and rowid_field_offset) starts from 1 */
1145
share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
1131
share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
1151
1138
share->primary_key = MAX_KEY; // we do not have a primary key
1448
1453
bitmap_size= share->column_bitmap_size;
1449
1454
if (!(bitmaps= (unsigned char*) alloc_root(&outparam->mem_root, bitmap_size*3)))
1451
outparam->def_read_set.init((my_bitmap_map*) bitmaps, share->fields);
1452
outparam->def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), share->fields);
1453
outparam->tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), share->fields);
1456
bitmap_init(&outparam->def_read_set,
1457
(my_bitmap_map*) bitmaps, share->fields);
1458
bitmap_init(&outparam->def_write_set,
1459
(my_bitmap_map*) (bitmaps+bitmap_size), share->fields);
1460
bitmap_init(&outparam->tmp_set,
1461
(my_bitmap_map*) (bitmaps+bitmap_size*2), share->fields);
1454
1462
outparam->default_column_bitmaps();
1456
1464
/* The table struct is now initialized; Open the table */
1466
if (db_stat && open_mode != OTM_ALTER)
1461
if ((ha_err= (outparam->cursor->
1469
if ((ha_err= (outparam->file->
1462
1470
ha_open(outparam, share->normalized_path.str,
1463
1471
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1464
1472
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
1560
void Table::resetTable(Session *session,
1562
uint32_t db_stat_arg)
1575
db_stat= db_stat_arg;
1578
record[0]= (unsigned char *) NULL;
1579
record[1]= (unsigned char *) NULL;
1581
insert_values= NULL;
1583
next_number_field= NULL;
1584
found_next_number_field= NULL;
1585
timestamp_field= NULL;
1587
pos_in_table_list= NULL;
1597
derived_select_number= 0;
1598
current_lock= F_UNLCK;
1612
open_placeholder= false;
1613
locked_by_name= false;
1616
auto_increment_field_not_null= false;
1617
alias_name_used= false;
1620
quick_condition_rows= 0;
1622
timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1627
covering_keys.reset();
1632
keys_in_use_for_query.reset();
1633
keys_in_use_for_group_by.reset();
1634
keys_in_use_for_order_by.reset();
1636
memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
1637
memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
1639
memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
1640
memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
1642
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
1643
memset(&sort, 0, sizeof(filesort_info_st));
1648
1569
/* Deallocate temporary blob storage */
1650
1571
void free_blobs(register Table *table)
1855
1778
uint32_t field_count= s->fields;
1857
this->def_read_set.init((my_bitmap_map*) bitmaps, field_count);
1858
this->tmp_set.init((my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), field_count);
1780
bitmap_init(&this->def_read_set, (my_bitmap_map*) bitmaps, field_count);
1781
bitmap_init(&this->tmp_set, (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), field_count);
1860
1783
/* write_set and all_set are copies of read_set */
1861
1784
def_write_set= def_read_set;
1862
1785
s->all_set= def_read_set;
1863
this->s->all_set.setAll();
1786
bitmap_set_all(&this->s->all_set);
1864
1787
default_column_bitmaps();
1869
void Table::updateCreateInfo(message::Table *table_proto)
1792
void Table::updateCreateInfo(HA_CREATE_INFO *create_info)
1871
message::Table::TableOptions *table_options= table_proto->mutable_options();
1872
table_options->set_block_size(s->block_size);
1873
table_options->set_comment(s->getComment());
1794
create_info->max_rows= s->max_rows;
1795
create_info->min_rows= s->min_rows;
1796
create_info->table_options= s->db_create_options;
1797
create_info->avg_row_length= s->avg_row_length;
1798
create_info->block_size= s->block_size;
1799
create_info->row_type= s->row_type;
1800
create_info->default_table_charset= s->table_charset;
1801
create_info->table_charset= 0;
1802
create_info->comment= s->comment;
1876
1807
int rename_file_ext(const char * from,const char * to,const char * ext)
2636
2616
/* If result table is small; use a heap */
2637
2617
/* future: storage engine selection can be made dynamic? */
2638
2618
if (blob_count || using_unique_constraint ||
2639
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
2619
(select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
2620
OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
2641
2622
share->storage_engine= myisam_engine;
2642
table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
2623
table->file= get_new_handler(share, &table->mem_root,
2644
(param->group_parts > table->cursor->getEngine()->max_key_parts() ||
2645
param->group_length > table->cursor->getEngine()->max_key_length()))
2646
using_unique_constraint= true;
2626
(param->group_parts > table->file->max_key_parts() ||
2627
param->group_length > table->file->max_key_length()))
2628
using_unique_constraint=1;
2650
2632
share->storage_engine= heap_engine;
2651
table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
2633
table->file= get_new_handler(share, &table->mem_root,
2653
if (! table->cursor)
2657
if (! using_unique_constraint)
2640
if (!using_unique_constraint)
2658
2641
reclength+= group_null_items; // null flag is stored separately
2660
2643
share->blob_fields= blob_count;
3350
3323
if (new_table.open_tmp_table())
3352
if (table->cursor->indexes_are_disabled())
3353
new_table.cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL);
3354
table->cursor->ha_index_or_rnd_end();
3355
table->cursor->ha_rnd_init(1);
3325
if (table->file->indexes_are_disabled())
3326
new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
3327
table->file->ha_index_or_rnd_end();
3328
table->file->ha_rnd_init(1);
3356
3329
if (table->no_rows)
3358
new_table.cursor->extra(HA_EXTRA_NO_ROWS);
3331
new_table.file->extra(HA_EXTRA_NO_ROWS);
3359
3332
new_table.no_rows=1;
3362
3335
/* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
3363
new_table.cursor->extra(HA_EXTRA_WRITE_CACHE);
3336
new_table.file->extra(HA_EXTRA_WRITE_CACHE);
3366
3339
copy all old rows from heap table to MyISAM table
3367
3340
This is the only code that uses record[1] to read/write but this
3368
3341
is safe as this is a temporary MyISAM table without timestamp/autoincrement.
3370
while (!table->cursor->rnd_next(new_table.record[1]))
3343
while (!table->file->rnd_next(new_table.record[1]))
3372
write_err= new_table.cursor->ha_write_row(new_table.record[1]);
3345
write_err= new_table.file->ha_write_row(new_table.record[1]);
3376
3349
/* copy row that filled HEAP table */
3377
if ((write_err=new_table.cursor->ha_write_row(table->record[0])))
3350
if ((write_err=new_table.file->ha_write_row(table->record[0])))
3379
if (new_table.cursor->is_fatal_error(write_err, HA_CHECK_DUP) ||
3352
if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
3380
3353
!ignore_last_dupp_key_error)
3384
3357
/* remove heap table and change to use myisam table */
3385
(void) table->cursor->ha_rnd_end();
3386
(void) table->cursor->close(); // This deletes the table !
3387
delete table->cursor;
3388
table->cursor= NULL;
3358
(void) table->file->ha_rnd_end();
3359
(void) table->file->close(); // This deletes the table !
3389
3362
new_table.s= table->s; // Keep old share
3390
3363
*table= new_table;
3391
3364
*table->s= share;
3393
table->cursor->change_table_ptr(table, table->s);
3366
table->file->change_table_ptr(table, table->s);
3394
3367
table->use_all_columns();
3395
3368
if (save_proc_info)
3546
3514
memset(null_flags, 255, s->null_bytes);
3560
insert_values(NULL),
3562
next_number_field(NULL),
3563
found_next_number_field(NULL),
3564
timestamp_field(NULL),
3565
pos_in_table_list(NULL),
3574
derived_select_number(0),
3575
current_lock(F_UNLCK),
3585
open_placeholder(false),
3586
locked_by_name(false),
3588
auto_increment_field_not_null(false),
3589
alias_name_used(false),
3591
quick_condition_rows(0),
3592
timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
3595
record[0]= (unsigned char *) 0;
3596
record[1]= (unsigned char *) 0;
3598
covering_keys.reset();
3603
keys_in_use_for_query.reset();
3604
keys_in_use_for_group_by.reset();
3605
keys_in_use_for_order_by.reset();
3607
memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
3608
memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
3610
memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
3611
memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
3613
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
3614
memset(&sort, 0, sizeof(filesort_info_st));
3617
3517
/*****************************************************************************
3618
3518
The different ways to read a record
3619
3519
Returns -1 if row was not found, 0 if row was found and 1 on errors
3620
3520
*****************************************************************************/
3622
/** Help function when we get some an error from the table Cursor. */
3522
/** Help function when we get some an error from the table handler. */
3624
3524
int Table::report_error(int error)
3661
3561
merge_keys.reset();
3666
Used by ALTER Table when the table is a temporary one. It changes something
3667
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
3669
Prepares a table cache key, which is the concatenation of db, table_name and
3670
session->slave_proxy_id, separated by '\0'.
3673
bool Table::renameAlterTemporaryTable(TableIdentifier &identifier)
3564
Field *Table::find_field_in_table_sef(const char *name)
3676
uint32_t key_length;
3677
TableShare *share= s;
3679
if (not (key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
3682
key_length= TableShare::createKey(key, identifier);
3683
share->set_table_cache_key(key, key_length);
3685
message::Table *message= share->getTableProto();
3687
message->set_name(identifier.getTableName());
3688
message->set_schema(identifier.getSchemaName());
3567
if (s->name_hash.records)
3569
field_ptr= (Field**)hash_search(&s->name_hash,(unsigned char*) name,
3574
field_ptr points to field in TableShare. Convert it to the matching
3577
field_ptr= (field + (field_ptr - s->field));
3582
if (!(field_ptr= field))
3584
for (; *field_ptr; ++field_ptr)
3585
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
3693
} /* namespace drizzled */
3594
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
3595
template class List<String>;
3596
template class List_iterator<String>;