13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
#include <drizzled/error.h>
18
#include <drizzled/session.h>
19
#include <drizzled/unireg.h>
20
#include "drizzled/sql_table.h"
21
#include "drizzled/global_charset_info.h"
23
#include "drizzled/internal/my_sys.h"
30
#include <drizzled/message/schema.pb.h>
31
#include <drizzled/message/table.pb.h>
32
#include <google/protobuf/io/zero_copy_stream.h>
33
#include <google/protobuf/io/zero_copy_stream_impl.h>
35
#include <drizzled/table_proto.h>
41
int fill_table_proto(message::Table *table_proto,
42
const char *table_name,
43
List<CreateField> &create_fields,
44
HA_CREATE_INFO *create_info,
48
CreateField *field_arg;
49
List_iterator<CreateField> it(create_fields);
50
message::Table::TableOptions *table_options= table_proto->mutable_options();
52
if (create_fields.elements > MAX_FIELDS)
54
my_error(ER_TOO_MANY_FIELDS, MYF(0), ER(ER_TOO_MANY_FIELDS));
58
assert(strcmp(table_proto->engine().name().c_str(),
59
create_info->db_type->getName().c_str())==0);
61
assert(strcmp(table_proto->name().c_str(),table_name)==0);
64
bool use_existing_fields= table_proto->field_size() > 0;
65
while ((field_arg= it++))
67
message::Table::Field *attribute;
69
/* some (one) code path for CREATE TABLE fills the proto
70
out more than the others, so we already have partially
71
filled out Field messages */
73
if (use_existing_fields)
74
attribute= table_proto->mutable_field(field_number++);
18
Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo
20
In the following functions FIELD * is an ordinary field-structure with
21
the following exeptions:
22
sc_length,typepos,row,kol,dtype,regnr and field need not to be set.
23
str is a (long) to record position where 0 is the first position.
26
#include "mysql_priv.h"
30
#define FCOMP 17 /* Bytes for a packed field */
32
static uchar * pack_screens(List<Create_field> &create_fields,
33
uint *info_length, uint *screens, bool small_file);
34
static uint pack_keys(uchar *keybuff,uint key_count, KEY *key_info,
36
static bool pack_header(uchar *forminfo,enum legacy_db_type table_type,
37
List<Create_field> &create_fields,
38
uint info_length, uint screens, uint table_options,
39
ulong data_offset, handler *file);
40
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
41
Create_field *last_field);
42
static bool pack_fields(File file, List<Create_field> &create_fields,
44
static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type,
46
List<Create_field> &create_fields,
47
uint reclength, ulong data_offset,
51
An interceptor to hijack ER_TOO_MANY_FIELDS error from
52
pack_screens and retry again without UNIREG screens.
54
XXX: what is a UNIREG screen?
57
struct Pack_header_error_handler: public Internal_error_handler
59
virtual bool handle_error(uint sql_errno,
61
MYSQL_ERROR::enum_warning_level level,
64
Pack_header_error_handler() :is_handled(FALSE) {}
69
Pack_header_error_handler::
70
handle_error(uint sql_errno,
71
const char * /* message */,
72
MYSQL_ERROR::enum_warning_level /* level */,
75
is_handled= (sql_errno == ER_TOO_MANY_FIELDS);
80
Create a frm (table definition) file
85
file_name Path for file (including database and .frm)
88
create_info create info parameters
89
create_fields Fields to create
90
keys number of keys to create
91
key_info Keys to create
92
db_file Handler to use. May be zero, in which case we use
99
bool mysql_create_frm(THD *thd, const char *file_name,
100
const char *db, const char *table,
101
HA_CREATE_INFO *create_info,
102
List<Create_field> &create_fields,
103
uint keys, KEY *key_info,
106
LEX_STRING str_db_type;
107
uint reclength, info_length, screens, key_info_length, maxlength, tmp_len;
108
ulong key_buff_length;
110
ulong filepos, data_offset;
111
uchar fileinfo[64],forminfo[288],*keybuff;
115
const uint format_section_header_size= 8;
116
uint format_section_len;
117
uint tablespace_len= 0;
118
Pack_header_error_handler pack_header_error_handler;
120
DBUG_ENTER("mysql_create_frm");
122
DBUG_ASSERT(*fn_rext((char*)file_name)); // Check .frm extension
123
formnames.type_names=0;
124
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0)))
126
DBUG_ASSERT(db_file != NULL);
128
/* If fixed row records, we need one bit to check for deleted rows */
129
if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
130
create_info->null_bits++;
131
data_offset= (create_info->null_bits + 7) / 8;
133
thd->push_internal_handler(&pack_header_error_handler);
135
error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
136
create_fields,info_length,
137
screens, create_info->table_options,
138
data_offset, db_file);
140
thd->pop_internal_handler();
144
my_free(screen_buff, MYF(0));
145
if (! pack_header_error_handler.is_handled)
148
// Try again without UNIREG screens (to get more columns)
149
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1)))
151
if (pack_header(forminfo, ha_legacy_type(create_info->db_type),
152
create_fields,info_length,
153
screens, create_info->table_options, data_offset, db_file))
77
/* Other code paths still have to fill out the proto */
78
attribute= table_proto->add_field();
80
if(field_arg->flags & NOT_NULL_FLAG)
82
message::Table::Field::FieldConstraints *constraints;
84
constraints= attribute->mutable_constraints();
85
constraints->set_is_nullable(false);
88
attribute->set_name(field_arg->field_name);
91
assert((!(field_arg->flags & NOT_NULL_FLAG)) == attribute->constraints().is_nullable());
92
assert(strcmp(attribute->name().c_str(), field_arg->field_name)==0);
95
message::Table::Field::FieldType parser_type= attribute->type();
97
switch (field_arg->sql_type) {
98
case DRIZZLE_TYPE_LONG:
99
attribute->set_type(message::Table::Field::INTEGER);
101
case DRIZZLE_TYPE_DOUBLE:
103
attribute->set_type(drizzled::message::Table::Field::DOUBLE);
106
* For DOUBLE, we only add a specific scale and precision iff
107
* the fixed decimal point has been specified...
109
if (field_arg->decimals != NOT_FIXED_DEC)
111
drizzled::message::Table::Field::NumericFieldOptions *numeric_field_options;
113
numeric_field_options= attribute->mutable_numeric_options();
115
numeric_field_options->set_precision(field_arg->length);
116
numeric_field_options->set_scale(field_arg->decimals);
120
case DRIZZLE_TYPE_NULL :
121
assert(1); /* Not a user definable type */
122
case DRIZZLE_TYPE_TIMESTAMP:
123
attribute->set_type(message::Table::Field::TIMESTAMP);
125
case DRIZZLE_TYPE_LONGLONG:
126
attribute->set_type(message::Table::Field::BIGINT);
128
case DRIZZLE_TYPE_DATETIME:
129
attribute->set_type(message::Table::Field::DATETIME);
131
case DRIZZLE_TYPE_DATE:
132
attribute->set_type(message::Table::Field::DATE);
134
case DRIZZLE_TYPE_VARCHAR:
136
message::Table::Field::StringFieldOptions *string_field_options;
138
string_field_options= attribute->mutable_string_options();
139
attribute->set_type(message::Table::Field::VARCHAR);
140
if (! use_existing_fields || string_field_options->length()==0)
141
string_field_options->set_length(field_arg->length
142
/ field_arg->charset->mbmaxlen);
144
assert((uint32_t)string_field_options->length() == (uint32_t)(field_arg->length / field_arg->charset->mbmaxlen));
146
if (! string_field_options->has_collation())
148
string_field_options->set_collation_id(field_arg->charset->number);
149
string_field_options->set_collation(field_arg->charset->name);
153
case DRIZZLE_TYPE_DECIMAL:
155
message::Table::Field::NumericFieldOptions *numeric_field_options;
157
attribute->set_type(message::Table::Field::DECIMAL);
158
numeric_field_options= attribute->mutable_numeric_options();
159
/* This is magic, I hate magic numbers -Brian */
160
numeric_field_options->set_precision(field_arg->length + ( field_arg->decimals ? -2 : -1));
161
numeric_field_options->set_scale(field_arg->decimals);
164
case DRIZZLE_TYPE_ENUM:
166
message::Table::Field::SetFieldOptions *set_field_options;
168
assert(field_arg->interval);
170
attribute->set_type(message::Table::Field::ENUM);
171
set_field_options= attribute->mutable_set_options();
173
for (uint32_t pos= 0; pos < field_arg->interval->count; pos++)
175
const char *src= field_arg->interval->type_names[pos];
177
set_field_options->add_field_value(src);
179
set_field_options->set_count_elements(set_field_options->field_value_size());
180
set_field_options->set_collation_id(field_arg->charset->number);
181
set_field_options->set_collation(field_arg->charset->name);
184
case DRIZZLE_TYPE_BLOB:
186
attribute->set_type(message::Table::Field::BLOB);
188
message::Table::Field::StringFieldOptions *string_field_options;
190
string_field_options= attribute->mutable_string_options();
191
string_field_options->set_collation_id(field_arg->charset->number);
192
string_field_options->set_collation(field_arg->charset->name);
197
assert(0); /* Tell us, since this shouldn't happend */
200
assert (!use_existing_fields || parser_type == attribute->type());
203
field_constraints= attribute->mutable_constraints();
204
constraints->set_is_nullable(field_arg->def->null_value);
155
my_free(screen_buff, MYF(0));
159
reclength=uint2korr(forminfo+266);
161
/* Calculate extra data segment length */
162
str_db_type.str= (char *) ha_resolve_storage_engine_name(create_info->db_type);
163
str_db_type.length= strlen(str_db_type.str);
165
create_info->extra_size= (2 + str_db_type.length +
166
2 + create_info->connect_string.length);
169
Length of partition info = 4 byte
170
Potential NULL byte at end of partition info string = 1 byte
171
Indicator if auto-partitioned table = 1 byte
174
create_info->extra_size+= 6;
176
/* Add space for storage type and field format array of fields */
177
if (create_info->tablespace)
178
tablespace_len= strlen(create_info->tablespace);
180
format_section_header_size +
182
create_fields.elements;
183
create_info->extra_size+= format_section_len;
185
tmp_len= system_charset_info->cset->charpos(system_charset_info,
186
create_info->comment.str,
187
create_info->comment.str +
188
create_info->comment.length,
189
TABLE_COMMENT_MAXLEN);
191
if (tmp_len < create_info->comment.length)
193
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
194
create_info->comment.str,"TABLE COMMENT",
195
(uint) TABLE_COMMENT_MAXLEN);
196
my_free(screen_buff,MYF(0));
200
//if table comment is larger than 180 bytes, store into extra segment.
201
if (create_info->comment.length > 180)
204
create_info->extra_size+= 2 + create_info->comment.length;
207
strmake((char*) forminfo+47, create_info->comment.str ?
208
create_info->comment.str : "", create_info->comment.length);
209
forminfo[46]=(uchar) create_info->comment.length;
212
EXTRA_DEBUG causes strmake() to initialize its buffer behind the
213
payload with a magic value to detect wrong buffer-sizes. We
214
explicitly zero that segment again.
216
memset((char*) forminfo+47 + forminfo[46], 0, 61 - forminfo[46]);
207
switch(field_arg->column_format())
209
case COLUMN_FORMAT_TYPE_NOT_USED:
211
case COLUMN_FORMAT_TYPE_DEFAULT:
212
attribute->set_format(message::Table::Field::DefaultFormat);
214
case COLUMN_FORMAT_TYPE_FIXED:
215
attribute->set_format(message::Table::Field::FixedFormat);
217
case COLUMN_FORMAT_TYPE_DYNAMIC:
218
attribute->set_format(message::Table::Field::DynamicFormat);
221
assert(0); /* Tell us, since this shouldn't happend */
224
if (field_arg->comment.length)
227
tmp_len= system_charset_info->cset->charpos(system_charset_info,
228
field_arg->comment.str,
229
field_arg->comment.str +
230
field_arg->comment.length,
231
COLUMN_COMMENT_MAXLEN);
233
if (tmp_len < field_arg->comment.length)
235
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
236
field_arg->comment.str,"COLUMN COMMENT",
237
(uint32_t) COLUMN_COMMENT_MAXLEN);
241
if (! use_existing_fields)
242
attribute->set_comment(field_arg->comment.str);
244
assert(strcmp(attribute->comment().c_str(), field_arg->comment.str)==0);
247
if(field_arg->unireg_check == Field::NEXT_NUMBER)
249
message::Table::Field::NumericFieldOptions *field_options;
250
field_options= attribute->mutable_numeric_options();
251
field_options->set_is_autoincrement(true);
254
if(field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
255
|| field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
257
message::Table::Field::FieldOptions *field_options;
258
field_options= attribute->mutable_options();
259
field_options->set_default_value("NOW()");
262
if(field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
263
|| field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD)
265
message::Table::Field::FieldOptions *field_options;
266
field_options= attribute->mutable_options();
267
field_options->set_update_value("NOW()");
272
message::Table::Field::FieldOptions *field_options;
273
field_options= attribute->mutable_options();
275
if(field_arg->def->is_null())
277
field_options->set_default_null(true);
282
String *default_value= field_arg->def->val_str(&d);
284
assert(default_value);
286
if((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
287
|| field_arg->sql_type==DRIZZLE_TYPE_BLOB)
288
&& ((field_arg->length / field_arg->charset->mbmaxlen)
289
< default_value->length()))
291
my_error(ER_INVALID_DEFAULT, MYF(0), field_arg->field_name);
295
if((field_arg->sql_type==DRIZZLE_TYPE_VARCHAR
296
&& field_arg->charset==&my_charset_bin)
297
|| (field_arg->sql_type==DRIZZLE_TYPE_BLOB
298
&& field_arg->charset==&my_charset_bin))
301
bin_default.assign(default_value->c_ptr(),
302
default_value->length());
303
field_options->set_default_bin_value(bin_default);
307
field_options->set_default_value(default_value->c_ptr());
312
assert(field_arg->unireg_check == Field::NONE
313
|| field_arg->unireg_check == Field::NEXT_NUMBER
314
|| field_arg->unireg_check == Field::TIMESTAMP_DN_FIELD
315
|| field_arg->unireg_check == Field::TIMESTAMP_UN_FIELD
316
|| field_arg->unireg_check == Field::TIMESTAMP_DNUN_FIELD);
320
assert(! use_existing_fields || (field_number == table_proto->field_size()));
322
switch(create_info->row_type)
324
case ROW_TYPE_DEFAULT:
325
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_DEFAULT);
328
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_FIXED);
330
case ROW_TYPE_DYNAMIC:
331
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_DYNAMIC);
333
case ROW_TYPE_COMPRESSED:
334
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_COMPRESSED);
336
case ROW_TYPE_REDUNDANT:
337
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_REDUNDANT);
339
case ROW_TYPE_COMPACT:
340
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_COMPACT);
343
table_options->set_row_type(message::Table::TableOptions::ROW_TYPE_PAGE);
349
table_options->set_pack_record(create_info->table_options
350
& HA_OPTION_PACK_RECORD);
352
if (table_options->has_comment())
355
tmp_len= system_charset_info->cset->charpos(system_charset_info,
356
table_options->comment().c_str(),
357
table_options->comment().c_str() +
358
table_options->comment().length(),
359
TABLE_COMMENT_MAXLEN);
361
if (tmp_len < table_options->comment().length())
363
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
364
table_options->comment().c_str(),"Table COMMENT",
365
(uint32_t) TABLE_COMMENT_MAXLEN);
370
if (create_info->default_table_charset)
372
table_options->set_collation_id(
373
create_info->default_table_charset->number);
374
table_options->set_collation(create_info->default_table_charset->name);
377
if (create_info->auto_increment_value)
378
table_options->set_auto_increment_value(create_info->auto_increment_value);
380
for (unsigned int i= 0; i < keys; i++)
382
message::Table::Index *idx;
384
idx= table_proto->add_indexes();
386
assert(test(key_info[i].flags & HA_USES_COMMENT) ==
387
(key_info[i].comment.length > 0));
389
idx->set_name(key_info[i].name);
391
idx->set_key_length(key_info[i].key_length);
393
if(is_primary_key_name(key_info[i].name))
394
idx->set_is_primary(true);
396
idx->set_is_primary(false);
398
switch(key_info[i].algorithm)
400
case HA_KEY_ALG_HASH:
401
idx->set_type(message::Table::Index::HASH);
404
case HA_KEY_ALG_BTREE:
405
idx->set_type(message::Table::Index::BTREE);
408
case HA_KEY_ALG_UNDEF:
409
idx->set_type(message::Table::Index::UNKNOWN_INDEX);
413
abort(); /* Somebody's brain broke. haven't added index type to proto */
416
if (key_info[i].flags & HA_NOSAME)
417
idx->set_is_unique(true);
419
idx->set_is_unique(false);
421
message::Table::Index::IndexOptions *index_options= idx->mutable_options();
423
if(key_info[i].flags & HA_USES_BLOCK_SIZE)
424
index_options->set_key_block_size(key_info[i].block_size);
426
if(key_info[i].flags & HA_PACK_KEY)
427
index_options->set_pack_key(true);
429
if(key_info[i].flags & HA_BINARY_PACK_KEY)
430
index_options->set_binary_pack_key(true);
432
if(key_info[i].flags & HA_VAR_LENGTH_PART)
433
index_options->set_var_length_key(true);
435
if(key_info[i].flags & HA_NULL_PART_KEY)
436
index_options->set_null_part_key(true);
438
if(key_info[i].flags & HA_KEY_HAS_PART_KEY_SEG)
439
index_options->set_has_partial_segments(true);
441
if(key_info[i].flags & HA_GENERATED_KEY)
442
index_options->set_auto_generated_key(true);
444
if (key_info[i].flags & HA_USES_COMMENT)
447
tmp_len= system_charset_info->cset->charpos(system_charset_info,
448
key_info[i].comment.str,
449
key_info[i].comment.str +
450
key_info[i].comment.length,
451
TABLE_COMMENT_MAXLEN);
453
if (tmp_len < key_info[i].comment.length)
455
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
456
key_info[i].comment.str,"Index COMMENT",
457
(uint32_t) TABLE_COMMENT_MAXLEN);
461
idx->set_comment(key_info[i].comment.str);
463
if(key_info[i].flags & ~(HA_NOSAME | HA_PACK_KEY | HA_USES_BLOCK_SIZE | HA_BINARY_PACK_KEY | HA_VAR_LENGTH_PART | HA_NULL_PART_KEY | HA_KEY_HAS_PART_KEY_SEG | HA_GENERATED_KEY | HA_USES_COMMENT))
464
abort(); // Invalid (unknown) index flag.
466
for(unsigned int j=0; j< key_info[i].key_parts; j++)
468
message::Table::Index::IndexPart *idxpart;
470
idxpart= idx->add_index_part();
472
idxpart->set_fieldnr(key_info[i].key_part[j].fieldnr);
474
idxpart->set_compare_length(key_info[i].key_part[j].length);
476
idxpart->set_key_type(key_info[i].key_part[j].key_type);
484
int rename_table_proto_file(const char *from, const char* to)
486
string from_path(from);
488
string file_ext = ".dfe";
490
from_path.append(file_ext);
491
to_path.append(file_ext);
493
return my_rename(from_path.c_str(),to_path.c_str(),MYF(MY_WME));
496
int delete_table_proto_file(const char *file_name)
498
string new_path(file_name);
499
string file_ext = ".dfe";
501
new_path.append(file_ext);
502
return my_delete(new_path.c_str(), MYF(0));
505
int drizzle_write_proto_file(const std::string file_name,
506
message::Table *table_proto)
508
int fd= open(file_name.c_str(), O_RDWR|O_CREAT|O_TRUNC, my_umask);
513
google::protobuf::io::ZeroCopyOutputStream* output=
514
new google::protobuf::io::FileOutputStream(fd);
516
if (table_proto->SerializeToZeroCopyStream(output) == false)
220
if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
221
create_info, keys, key_info)) < 0)
223
my_free(screen_buff, MYF(0));
227
key_buff_length= uint4korr(fileinfo+47);
228
keybuff=(uchar*) my_malloc(key_buff_length, MYF(0));
229
key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
230
VOID(get_form_pos(file,fileinfo,&formnames));
231
if (!(filepos=make_new_entry(file,fileinfo,&formnames,"")))
233
maxlength=(uint) next_io_size((ulong) (uint2korr(forminfo)+1000));
234
int2store(forminfo+2,maxlength);
235
int4store(fileinfo+10,(ulong) (filepos+maxlength));
236
fileinfo[26]= (uchar) test((create_info->max_rows == 1) &&
237
(create_info->min_rows == 1) && (keys == 0));
238
int2store(fileinfo+28,key_info_length);
241
int2store(fileinfo+59,db_file->extra_rec_buf_length());
243
if (my_pwrite(file, fileinfo, 64, 0L, MYF_RW) ||
244
my_pwrite(file, keybuff, key_info_length,
245
(ulong) uint2korr(fileinfo+6),MYF_RW))
248
(ulong) uint2korr(fileinfo+6)+ (ulong) key_buff_length,
249
MY_SEEK_SET,MYF(0)));
250
if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
251
create_info->table_options,
252
create_fields,reclength, data_offset, db_file))
255
int2store(buff, create_info->connect_string.length);
256
if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
257
my_write(file, (const uchar*)create_info->connect_string.str,
258
create_info->connect_string.length, MYF(MY_NABP)))
261
int2store(buff, str_db_type.length);
262
if (my_write(file, (const uchar*)buff, 2, MYF(MY_NABP)) ||
263
my_write(file, (const uchar*)str_db_type.str,
264
str_db_type.length, MYF(MY_NABP)))
268
bzero((uchar*) buff, 6);
269
if (my_write(file, (uchar*) buff, 6, MYF_RW))
273
if (forminfo[46] == (uchar)255)
275
uchar comment_length_buff[2];
276
int2store(comment_length_buff,create_info->comment.length);
277
if (my_write(file, comment_length_buff, 2, MYF(MY_NABP)) ||
278
my_write(file, (uchar*)create_info->comment.str,
279
create_info->comment.length, MYF(MY_NABP)))
283
/* Store storage type and field format array of fields */
288
flags|= create_info->default_storage_media; //3 bits
290
bzero(buff, format_section_header_size);
291
/* length of section 2 bytes*/
292
int2store(buff+0, format_section_len);
293
/* flags of section 4 bytes*/
294
int4store(buff+2, flags);
295
/* 2 bytes left for future use */
298
if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW))
300
/* write tablespace name */
301
if (tablespace_len > 0)
302
if (my_write(file, (const uchar*)create_info->tablespace, tablespace_len, MYF_RW))
305
if (my_write(file, (const uchar*)buff, 1, MYF_RW))
307
/* write column info, 1 byte per column */
309
List_iterator<Create_field> it(create_fields);
311
uchar storage_type, column_format, write_byte;
314
storage_type= (uchar)field->field_storage_type();
315
column_format= (uchar)field->column_format();
316
write_byte= storage_type + (column_format << COLUMN_FORMAT_SHIFT);
317
if (my_write(file, &write_byte, 1, MYF_RW))
322
VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
323
if (my_write(file, forminfo, 288, MYF_RW) ||
324
my_write(file, screen_buff, info_length, MYF_RW) ||
325
pack_fields(file, create_fields, data_offset))
328
my_free(screen_buff,MYF(0));
329
my_free(keybuff, MYF(0));
331
if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
332
(my_sync(file, MYF(MY_WME)) ||
333
my_sync_dir_by_file(file_name, MYF(MY_WME))))
336
if (my_close(file,MYF(MY_WME)))
341
Restore all UCS2 intervals.
342
HEX representation of them is not needed anymore.
344
List_iterator<Create_field> it(create_fields);
348
if (field->save_interval)
350
field->interval= field->save_interval;
351
field->save_interval= 0;
358
my_free(screen_buff, MYF(0));
359
my_free(keybuff, MYF(0));
361
VOID(my_close(file,MYF(MY_WME)));
363
my_delete(file_name,MYF(0));
365
} /* mysql_create_frm */
529
Create a table definition proto file and the tables
369
Create a frm (table definition) file and the tables
532
372
rea_create_table()
533
session Thread handler
534
374
path Name of file (including database, without .frm)
535
375
db Data base name
536
376
table_name Table name
538
378
create_fields Fields to create
539
379
keys number of keys to create
540
380
key_info Keys to create
547
int rea_create_table(Session *session,
548
TableIdentifier &identifier,
549
message::Table *table_proto,
388
int rea_create_table(THD *thd, const char *path,
389
const char *db, const char *table_name,
550
390
HA_CREATE_INFO *create_info,
551
List<CreateField> &create_fields,
552
uint32_t keys, KEY *key_info)
391
List<Create_field> &create_fields,
392
uint keys, KEY *key_info, handler *file)
554
if (fill_table_proto(table_proto, identifier.getTableName(), create_fields, create_info,
558
string new_path(identifier.getPath());
559
string file_ext = ".dfe";
561
new_path.append(file_ext);
565
plugin::StorageEngine* engine= plugin::StorageEngine::findByName(*session,
566
table_proto->engine().name());
567
if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
568
err= drizzle_write_proto_file(new_path, table_proto);
573
my_error(ER_BAD_DB_ERROR,MYF(0), identifier.getDBName());
575
my_error(ER_CANT_CREATE_TABLE, MYF(0), identifier.getTableName(), err);
580
if (plugin::StorageEngine::createTable(*session,
582
false, *table_proto))
394
DBUG_ENTER("rea_create_table");
396
char frm_name[FN_REFLEN];
397
strxmov(frm_name, path, reg_ext, NullS);
398
if (mysql_create_frm(thd, frm_name, db, table_name, create_info,
399
create_fields, keys, key_info, file))
403
// Make sure mysql_create_frm din't remove extension
404
DBUG_ASSERT(*fn_rext(frm_name));
405
if (thd->variables.keep_files_on_create)
406
create_info->options|= HA_CREATE_KEEP_FILES;
407
if (file->ha_create_handler_files(path, NULL, CHF_CREATE_FLAG, create_info))
409
if (!create_info->frm_only && ha_create_table(thd, path, db, table_name,
587
if (engine->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == false)
588
plugin::StorageEngine::deleteDefinitionFromPath(identifier);
415
VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info));
416
my_delete(frm_name, MYF(0));
591
418
} /* rea_create_table */
593
} /* namespace drizzled */
421
/* Pack screens to a screen for save in a form-file */
423
static uchar *pack_screens(List<Create_field> &create_fields,
424
uint *info_length, uint *screens,
428
uint row,start_row,end_row,fields_on_screen;
430
uchar *info,*pos,*start_screen;
431
uint fields=create_fields.elements;
432
List_iterator<Create_field> it(create_fields);
433
DBUG_ENTER("pack_screens");
435
start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row;
437
*screens=(fields-1)/fields_on_screen+1;
438
length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4);
442
length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2;
444
if (!(info=(uchar*) my_malloc(length,MYF(MY_WME))))
451
for (i=0 ; i < fields ; i++)
453
Create_field *cfield=it++;
454
if (row++ == end_row)
458
length=(uint) (pos-start_screen);
459
int2store(start_screen,length);
460
start_screen[2]=(uchar) (fields_on_screen+1);
461
start_screen[3]=(uchar) (fields_on_screen);
466
pos[0]= (uchar) start_row-2; /* Header string */
467
pos[1]= (uchar) (cols >> 2);
468
pos[2]= (uchar) (cols >> 1) +1;
469
strfill((char *) pos+3,(uint) (cols >> 1),' ');
472
length=(uint) strlen(cfield->field_name);
480
pos[2]=(uchar) (length+1);
481
pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1;
483
cfield->row=(uint8) row;
484
cfield->col=(uint8) (length+1);
485
cfield->sc_length=(uint8) min(cfield->length,cols-(length+2));
487
length=(uint) (pos-start_screen);
488
int2store(start_screen,length);
489
start_screen[2]=(uchar) (row-start_row+2);
490
start_screen[3]=(uchar) (row-start_row+1);
492
*info_length=(uint) (pos-info);
497
/* Pack keyinfo and keynames to keybuff for save in form-file. */
499
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
502
uint key_parts,length;
503
uchar *pos, *keyname_pos;
505
KEY_PART_INFO *key_part,*key_part_end;
506
DBUG_ENTER("pack_keys");
510
for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
512
int2store(pos, (key->flags ^ HA_NOSAME));
513
int2store(pos+2,key->key_length);
514
pos[4]= (uchar) key->key_parts;
515
pos[5]= (uchar) key->algorithm;
516
int2store(pos+6, key->block_size);
518
key_parts+=key->key_parts;
519
DBUG_PRINT("loop", ("flags: %lu key_parts: %d at 0x%lx",
520
key->flags, key->key_parts,
521
(long) key->key_part));
522
for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
523
key_part != key_part_end ;
528
DBUG_PRINT("loop",("field: %d startpos: %lu length: %d",
529
key_part->fieldnr, key_part->offset + data_offset,
531
int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
532
offset= (uint) (key_part->offset+data_offset+1);
533
int2store(pos+2, offset);
534
pos[4]=0; // Sort order
535
int2store(pos+5,key_part->key_type);
536
int2store(pos+7,key_part->length);
542
*pos++=(uchar) NAMES_SEP_CHAR;
543
for (key=keyinfo ; key != end ; key++)
545
uchar *tmp=(uchar*) strmov((char*) pos,key->name);
546
*tmp++= (uchar) NAMES_SEP_CHAR;
552
for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
554
if (key->flags & HA_USES_COMMENT)
556
int2store(pos, key->comment.length);
557
uchar *tmp= (uchar*)strnmov((char*) pos+2,key->comment.str,key->comment.length);
562
if (key_count > 127 || key_parts > 127)
564
keybuff[0]= (key_count & 0x7f) | 0x80;
565
keybuff[1]= key_count >> 7;
566
int2store(keybuff+2,key_parts);
570
keybuff[0]=(uchar) key_count;
571
keybuff[1]=(uchar) key_parts;
572
keybuff[2]= keybuff[3]= 0;
574
length=(uint) (pos-keyname_pos);
575
int2store(keybuff+4,length);
576
DBUG_RETURN((uint) (pos-keybuff));
580
/* Make formheader */
582
static bool pack_header(uchar *forminfo, enum legacy_db_type table_type,
583
List<Create_field> &create_fields,
584
uint info_length, uint screens, uint table_options,
585
ulong data_offset, handler *file)
587
uint length,int_count,int_length,no_empty, int_parts;
588
uint time_stamp_pos,null_fields;
589
ulong reclength, totlength, n_length, com_length;
590
DBUG_ENTER("pack_header");
592
if (create_fields.elements > MAX_FIELDS)
594
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
599
reclength= data_offset;
600
no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields=
606
List_iterator<Create_field> it(create_fields);
610
uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
613
field->comment.length,
614
COLUMN_COMMENT_MAXLEN);
616
if (tmp_len < field->comment.length)
618
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
619
field->comment.str,"COLUMN COMMENT",
620
(uint) COLUMN_COMMENT_MAXLEN);
624
totlength+= field->length;
625
com_length+= field->comment.length;
626
if (MTYP_TYPENR(field->unireg_check) == Field::NOEMPTY ||
627
field->unireg_check & MTYP_NOEMPTY_BIT)
629
field->unireg_check= (Field::utype) ((uint) field->unireg_check |
634
We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE
635
as auto-update field.
637
if (field->sql_type == MYSQL_TYPE_TIMESTAMP &&
638
MTYP_TYPENR(field->unireg_check) != Field::NONE &&
640
time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
641
length=field->pack_length;
642
/* Ensure we don't have any bugs when generating offsets */
643
DBUG_ASSERT(reclength == field->offset + data_offset);
644
if ((uint) field->offset+ (uint) data_offset+ length > reclength)
645
reclength=(uint) (field->offset+ data_offset + length);
646
n_length+= (ulong) strlen(field->field_name)+1;
647
field->interval_id=0;
648
field->save_interval= 0;
651
uint old_int_count=int_count;
653
if (field->charset->mbminlen > 1)
656
Escape UCS2 intervals using HEX notation to avoid
657
problems with delimiters between enum elements.
658
As the original representation is still needed in
659
the function make_empty_rec to create a record of
660
filled with default values it is saved in save_interval
661
The HEX representation is created from this copy.
663
field->save_interval= field->interval;
664
field->interval= (TYPELIB*) sql_alloc(sizeof(TYPELIB));
665
*field->interval= *field->save_interval;
666
field->interval->type_names=
667
(const char **) sql_alloc(sizeof(char*) *
668
(field->interval->count+1));
669
field->interval->type_names[field->interval->count]= 0;
670
field->interval->type_lengths=
671
(uint *) sql_alloc(sizeof(uint) * field->interval->count);
673
for (uint pos= 0; pos < field->interval->count; pos++)
676
const char *src= field->save_interval->type_names[pos];
678
length= field->save_interval->type_lengths[pos];
679
hex_length= length * 2;
680
field->interval->type_lengths[pos]= hex_length;
681
field->interval->type_names[pos]= dst= (char*) sql_alloc(hex_length +
683
octet2hex(dst, src, length);
687
field->interval_id=get_interval_id(&int_count,create_fields,field);
688
if (old_int_count != int_count)
690
for (const char **pos=field->interval->type_names ; *pos ; pos++)
691
int_length+=(uint) strlen(*pos)+1; // field + suffix prefix
692
int_parts+=field->interval->count+1;
695
if (f_maybe_null(field->pack_flag))
698
int_length+=int_count*2; // 255 prefix + 0 suffix
700
/* Save values in forminfo */
702
if (reclength > (ulong) file->max_record_length())
704
my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length());
707
/* Hack to avoid bugs with small static rows in MySQL */
708
reclength=max(file->min_record_length(table_options),reclength);
709
if (info_length+(ulong) create_fields.elements*FCOMP+288+
710
n_length+int_length+com_length > 65535L || int_count > 255)
712
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0));
716
bzero((char*)forminfo,288);
717
length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+
719
int2store(forminfo,length);
720
forminfo[256] = (uint8) screens;
721
int2store(forminfo+258,create_fields.elements);
722
int2store(forminfo+260,info_length);
723
int2store(forminfo+262,totlength);
724
int2store(forminfo+264,no_empty);
725
int2store(forminfo+266,reclength);
726
int2store(forminfo+268,n_length);
727
int2store(forminfo+270,int_count);
728
int2store(forminfo+272,int_parts);
729
int2store(forminfo+274,int_length);
730
int2store(forminfo+276,time_stamp_pos);
731
int2store(forminfo+278,80); /* Columns needed */
732
int2store(forminfo+280,22); /* Rows needed */
733
int2store(forminfo+282,null_fields);
734
int2store(forminfo+284,com_length);
735
/* Up to forminfo+288 is free to use for additional information */
740
/* get each unique interval each own id */
742
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
743
Create_field *last_field)
745
List_iterator<Create_field> it(create_fields);
747
TYPELIB *interval=last_field->interval;
749
while ((field=it++) != last_field)
751
if (field->interval_id && field->interval->count == interval->count)
754
for (a=field->interval->type_names, b=interval->type_names ;
755
*a && !strcmp(*a,*b);
760
return field->interval_id; // Re-use last interval
764
return ++*int_count; // New unique interval
768
/* Save fields, fieldnames and intervals */
770
static bool pack_fields(File file, List<Create_field> &create_fields,
774
uint int_count, comment_length=0;
775
uchar buff[MAX_FIELD_WIDTH];
777
DBUG_ENTER("pack_fields");
779
/* Write field info */
781
List_iterator<Create_field> it(create_fields);
787
buff[0]= (uchar) field->row;
788
buff[1]= (uchar) field->col;
789
buff[2]= (uchar) field->sc_length;
790
int2store(buff+3, field->length);
791
/* The +1 is here becasue the col offset in .frm file have offset 1 */
792
recpos= field->offset+1 + (uint) data_offset;
793
int3store(buff+5,recpos);
794
int2store(buff+8,field->pack_flag);
795
int2store(buff+10,field->unireg_check);
796
buff[12]= (uchar) field->interval_id;
797
buff[13]= (uchar) field->sql_type;
799
buff[14]= (uchar) field->charset->number;
801
buff[14]= 0; // Numerical
802
int2store(buff+15, field->comment.length);
803
comment_length+= field->comment.length;
804
set_if_bigger(int_count,field->interval_id);
805
if (my_write(file, buff, FCOMP, MYF_RW))
809
/* Write fieldnames */
810
buff[0]=(uchar) NAMES_SEP_CHAR;
811
if (my_write(file, buff, 1, MYF_RW))
817
char *pos= strmov((char*) buff,field->field_name);
818
*pos++=NAMES_SEP_CHAR;
819
if (i == create_fields.elements-1)
821
if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW))
826
/* Write intervals */
829
String tmp((char*) buff,sizeof(buff), &my_charset_bin);
835
if (field->interval_id > int_count)
837
unsigned char sep= 0;
838
unsigned char occ[256];
840
unsigned char *val= NULL;
842
bzero(occ, sizeof(occ));
844
for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
845
for (uint j = 0; j < field->interval->type_lengths[i]; j++)
846
occ[(unsigned int) (val[j])]= 1;
848
if (!occ[(unsigned char)NAMES_SEP_CHAR])
849
sep= (unsigned char) NAMES_SEP_CHAR;
850
else if (!occ[(unsigned int)','])
854
for (uint i=1; i<256; i++)
863
if(!sep) /* disaster, enum uses all characters, none left as separator */
865
my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
871
int_count= field->interval_id;
873
for (const char **pos=field->interval->type_names ; *pos ; pos++)
878
tmp.append('\0'); // End of intervall
881
if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW))
890
if (field->comment.length)
891
if (my_write(file, (uchar*) field->comment.str, field->comment.length,
900
/* save an empty record on start of formfile */
902
static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type,
904
List<Create_field> &create_fields,
912
uchar *buff,*null_pos;
916
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
917
DBUG_ENTER("make_empty_rec");
919
/* We need a table to generate columns for default values */
920
bzero((char*) &table, sizeof(table));
921
bzero((char*) &share, sizeof(share));
924
if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL))))
930
table.s->db_low_byte_first= handler->low_byte_first();
931
table.s->blob_ptr_size= portable_sizeof_char_ptr;
934
if (!(table_options & HA_OPTION_PACK_RECORD))
936
null_count++; // Need one bit for delete mark
941
List_iterator<Create_field> it(create_fields);
942
thd->count_cuted_fields= CHECK_FIELD_WARN; // To find wrong default values
946
regfield don't have to be deleted as it's allocated with sql_alloc()
948
Field *regfield= make_field(&share,
949
buff+field->offset + data_offset,
951
null_pos + null_count / 8,
957
field->save_interval ? field->save_interval :
963
goto err; // End of memory
966
/* save_in_field() will access regfield->table->in_use */
967
regfield->init(&table);
969
if (!(field->flags & NOT_NULL_FLAG))
971
*regfield->null_ptr|= regfield->null_bit;
975
if (field->sql_type == MYSQL_TYPE_BIT && !f_bit_as_char(field->pack_flag))
976
null_count+= field->length & 7;
978
type= (Field::utype) MTYP_TYPENR(field->unireg_check);
982
int res= field->def->save_in_field(regfield, 1);
983
/* If not ok or warning of level 'note' */
984
if (res != 0 && res != 3)
986
my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name);
988
delete regfield; //To avoid memory leak
992
else if (regfield->real_type() == MYSQL_TYPE_ENUM &&
993
(field->flags & NOT_NULL_FLAG))
995
regfield->set_notnull();
996
regfield->store((longlong) 1, TRUE);
998
else if (type == Field::YES) // Old unireg type
999
regfield->store(ER(ER_YES),(uint) strlen(ER(ER_YES)),system_charset_info);
1000
else if (type == Field::NO) // Old unireg type
1001
regfield->store(ER(ER_NO), (uint) strlen(ER(ER_NO)),system_charset_info);
1005
DBUG_ASSERT(data_offset == ((null_count + 7) / 8));
1008
We need to set the unused bits to 1. If the number of bits is a multiple
1009
of 8 there are no unused bits.
1012
*(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
1014
error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0;
1017
my_free(buff, MYF(MY_FAE));
1018
thd->count_cuted_fields= old_count_cuted_fields;
1020
} /* make_empty_rec */