1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2006 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
||
17 |
/*
|
|
18 |
Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo
|
|
19 |
struct.
|
|
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.
|
|
24 |
*/
|
|
25 |
||
26 |
#include "mysql_priv.h" |
|
27 |
#include <m_ctype.h> |
|
28 |
#include <assert.h> |
|
29 |
||
30 |
#define FCOMP 17 /* Bytes for a packed field */ |
|
31 |
||
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, |
|
35 |
ulong data_offset); |
|
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, |
|
43 |
ulong data_offset); |
|
44 |
static bool make_empty_rec(THD *thd, int file, enum legacy_db_type table_type, |
|
45 |
uint table_options, |
|
46 |
List<Create_field> &create_fields, |
|
47 |
uint reclength, ulong data_offset, |
|
48 |
handler *handler); |
|
49 |
||
50 |
/**
|
|
51 |
An interceptor to hijack ER_TOO_MANY_FIELDS error from
|
|
52 |
pack_screens and retry again without UNIREG screens.
|
|
53 |
||
54 |
XXX: what is a UNIREG screen?
|
|
55 |
*/
|
|
56 |
||
57 |
struct Pack_header_error_handler: public Internal_error_handler |
|
58 |
{
|
|
59 |
virtual bool handle_error(uint sql_errno, |
|
60 |
const char *message, |
|
61 |
MYSQL_ERROR::enum_warning_level level, |
|
62 |
THD *thd); |
|
63 |
bool is_handled; |
|
64 |
Pack_header_error_handler() :is_handled(FALSE) {} |
|
65 |
};
|
|
66 |
||
67 |
||
68 |
bool
|
|
69 |
Pack_header_error_handler:: |
|
70 |
handle_error(uint sql_errno, |
|
71 |
const char * /* message */, |
|
72 |
MYSQL_ERROR::enum_warning_level /* level */, |
|
73 |
THD * /* thd */) |
|
74 |
{
|
|
75 |
is_handled= (sql_errno == ER_TOO_MANY_FIELDS); |
|
76 |
return is_handled; |
|
77 |
}
|
|
78 |
||
79 |
/*
|
|
80 |
Create a frm (table definition) file
|
|
81 |
||
82 |
SYNOPSIS
|
|
83 |
mysql_create_frm()
|
|
84 |
thd Thread handler
|
|
85 |
file_name Path for file (including database and .frm)
|
|
86 |
db Name of database
|
|
87 |
table Name of table
|
|
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
|
|
93 |
create_info->db_type
|
|
94 |
RETURN
|
|
95 |
0 ok
|
|
96 |
1 error
|
|
97 |
*/
|
|
98 |
||
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, |
|
104 |
handler *db_file) |
|
105 |
{
|
|
106 |
LEX_STRING str_db_type; |
|
107 |
uint reclength, info_length, screens, key_info_length, maxlength, tmp_len; |
|
108 |
ulong key_buff_length; |
|
109 |
File file; |
|
110 |
ulong filepos, data_offset; |
|
111 |
uchar fileinfo[64],forminfo[288],*keybuff; |
|
112 |
TYPELIB formnames; |
|
113 |
uchar *screen_buff; |
|
114 |
char buff[128]; |
|
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; |
|
119 |
int error; |
|
120 |
DBUG_ENTER("mysql_create_frm"); |
|
121 |
||
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))) |
|
125 |
DBUG_RETURN(1); |
|
126 |
DBUG_ASSERT(db_file != NULL); |
|
127 |
||
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; |
|
132 |
||
133 |
thd->push_internal_handler(&pack_header_error_handler); |
|
134 |
||
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); |
|
139 |
||
140 |
thd->pop_internal_handler(); |
|
141 |
||
142 |
if (error) |
|
143 |
{
|
|
144 |
my_free(screen_buff, MYF(0)); |
|
145 |
if (! pack_header_error_handler.is_handled) |
|
146 |
DBUG_RETURN(1); |
|
147 |
||
148 |
// Try again without UNIREG screens (to get more columns)
|
|
149 |
if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,1))) |
|
150 |
DBUG_RETURN(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)) |
|
154 |
{
|
|
155 |
my_free(screen_buff, MYF(0)); |
|
156 |
DBUG_RETURN(1); |
|
157 |
}
|
|
158 |
}
|
|
159 |
reclength=uint2korr(forminfo+266); |
|
160 |
||
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); |
|
164 |
/* str_db_type */
|
|
165 |
create_info->extra_size= (2 + str_db_type.length + |
|
166 |
2 + create_info->connect_string.length); |
|
167 |
/*
|
|
168 |
Partition:
|
|
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
|
|
172 |
=> Total 6 byte
|
|
173 |
*/
|
|
174 |
create_info->extra_size+= 6; |
|
175 |
||
176 |
/* Add space for storage type and field format array of fields */
|
|
177 |
if (create_info->tablespace) |
|
178 |
tablespace_len= strlen(create_info->tablespace); |
|
179 |
format_section_len= |
|
180 |
format_section_header_size + |
|
181 |
tablespace_len + 1 + |
|
182 |
create_fields.elements; |
|
183 |
create_info->extra_size+= format_section_len; |
|
184 |
||
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); |
|
190 |
||
191 |
if (tmp_len < create_info->comment.length) |
|
192 |
{
|
|
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)); |
|
197 |
DBUG_RETURN(1); |
|
198 |
}
|
|
199 |
||
200 |
//if table comment is larger than 180 bytes, store into extra segment.
|
|
201 |
if (create_info->comment.length > 180) |
|
202 |
{
|
|
203 |
forminfo[46]=255; |
|
204 |
create_info->extra_size+= 2 + create_info->comment.length; |
|
205 |
}
|
|
206 |
else{ |
|
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; |
|
210 |
#ifdef EXTRA_DEBUG
|
|
211 |
/*
|
|
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.
|
|
215 |
*/
|
|
216 |
memset((char*) forminfo+47 + forminfo[46], 0, 61 - forminfo[46]); |
|
217 |
#endif
|
|
218 |
}
|
|
219 |
||
220 |
if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo, |
|
221 |
create_info, keys, key_info)) < 0) |
|
222 |
{
|
|
223 |
my_free(screen_buff, MYF(0)); |
|
224 |
DBUG_RETURN(1); |
|
225 |
}
|
|
226 |
||
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,""))) |
|
232 |
goto err; |
|
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); |
|
239 |
||
240 |
||
241 |
int2store(fileinfo+59,db_file->extra_rec_buf_length()); |
|
242 |
||
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)) |
|
246 |
goto err; |
|
247 |
VOID(my_seek(file, |
|
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)) |
|
253 |
goto err; |
|
254 |
||
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))) |
|
259 |
goto err; |
|
260 |
||
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))) |
|
265 |
goto err; |
|
266 |
||
267 |
{
|
|
268 |
bzero((uchar*) buff, 6); |
|
269 |
if (my_write(file, (uchar*) buff, 6, MYF_RW)) |
|
270 |
goto err; |
|
271 |
}
|
|
272 |
||
273 |
if (forminfo[46] == (uchar)255) |
|
274 |
{
|
|
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))) |
|
280 |
goto err; |
|
281 |
}
|
|
282 |
||
283 |
/* Store storage type and field format array of fields */
|
|
284 |
{
|
|
285 |
/* prepare header */
|
|
286 |
{
|
|
287 |
uint flags= 0; |
|
288 |
flags|= create_info->default_storage_media; //3 bits |
|
289 |
||
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 */
|
|
296 |
}
|
|
297 |
/* write header */
|
|
298 |
if (my_write(file, (const uchar*)buff, format_section_header_size, MYF_RW)) |
|
299 |
goto err; |
|
300 |
/* write tablespace name */
|
|
301 |
if (tablespace_len > 0) |
|
302 |
if (my_write(file, (const uchar*)create_info->tablespace, tablespace_len, MYF_RW)) |
|
303 |
goto err; |
|
304 |
buff[0]= 0; |
|
305 |
if (my_write(file, (const uchar*)buff, 1, MYF_RW)) |
|
306 |
goto err; |
|
307 |
/* write column info, 1 byte per column */
|
|
308 |
{
|
|
309 |
List_iterator<Create_field> it(create_fields); |
|
310 |
Create_field *field; |
|
311 |
uchar storage_type, column_format, write_byte; |
|
312 |
while ((field=it++)) |
|
313 |
{
|
|
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)) |
|
318 |
goto err; |
|
319 |
}
|
|
320 |
}
|
|
321 |
}
|
|
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)) |
|
326 |
goto err; |
|
327 |
||
328 |
my_free(screen_buff,MYF(0)); |
|
329 |
my_free(keybuff, MYF(0)); |
|
330 |
||
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)))) |
|
334 |
goto err2; |
|
335 |
||
336 |
if (my_close(file,MYF(MY_WME))) |
|
337 |
goto err3; |
|
338 |
||
339 |
{
|
|
340 |
/*
|
|
341 |
Restore all UCS2 intervals.
|
|
342 |
HEX representation of them is not needed anymore.
|
|
343 |
*/
|
|
344 |
List_iterator<Create_field> it(create_fields); |
|
345 |
Create_field *field; |
|
346 |
while ((field=it++)) |
|
347 |
{
|
|
348 |
if (field->save_interval) |
|
349 |
{
|
|
350 |
field->interval= field->save_interval; |
|
351 |
field->save_interval= 0; |
|
352 |
}
|
|
353 |
}
|
|
354 |
}
|
|
355 |
DBUG_RETURN(0); |
|
356 |
||
357 |
err: |
|
358 |
my_free(screen_buff, MYF(0)); |
|
359 |
my_free(keybuff, MYF(0)); |
|
360 |
err2: |
|
361 |
VOID(my_close(file,MYF(MY_WME))); |
|
362 |
err3: |
|
363 |
my_delete(file_name,MYF(0)); |
|
364 |
DBUG_RETURN(1); |
|
365 |
} /* mysql_create_frm */ |
|
366 |
||
367 |
||
368 |
/*
|
|
369 |
Create a frm (table definition) file and the tables
|
|
370 |
||
371 |
SYNOPSIS
|
|
372 |
rea_create_table()
|
|
373 |
thd Thread handler
|
|
374 |
path Name of file (including database, without .frm)
|
|
375 |
db Data base name
|
|
376 |
table_name Table name
|
|
377 |
create_info create info parameters
|
|
378 |
create_fields Fields to create
|
|
379 |
keys number of keys to create
|
|
380 |
key_info Keys to create
|
|
381 |
file Handler to use
|
|
382 |
||
383 |
RETURN
|
|
384 |
0 ok
|
|
385 |
1 error
|
|
386 |
*/
|
|
387 |
||
388 |
int rea_create_table(THD *thd, const char *path, |
|
389 |
const char *db, const char *table_name, |
|
390 |
HA_CREATE_INFO *create_info, |
|
391 |
List<Create_field> &create_fields, |
|
392 |
uint keys, KEY *key_info, handler *file) |
|
393 |
{
|
|
394 |
DBUG_ENTER("rea_create_table"); |
|
395 |
||
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)) |
|
400 |
||
401 |
DBUG_RETURN(1); |
|
402 |
||
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)) |
|
408 |
goto err_handler; |
|
409 |
if (!create_info->frm_only && ha_create_table(thd, path, db, table_name, |
|
410 |
create_info,0)) |
|
411 |
goto err_handler; |
|
412 |
DBUG_RETURN(0); |
|
413 |
||
414 |
err_handler: |
|
415 |
VOID(file->ha_create_handler_files(path, NULL, CHF_DELETE_FLAG, create_info)); |
|
416 |
my_delete(frm_name, MYF(0)); |
|
417 |
DBUG_RETURN(1); |
|
418 |
} /* rea_create_table */ |
|
419 |
||
420 |
||
421 |
/* Pack screens to a screen for save in a form-file */
|
|
422 |
||
423 |
static uchar *pack_screens(List<Create_field> &create_fields, |
|
424 |
uint *info_length, uint *screens, |
|
425 |
bool small_file) |
|
426 |
{
|
|
427 |
register uint i; |
|
428 |
uint row,start_row,end_row,fields_on_screen; |
|
429 |
uint length,cols; |
|
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"); |
|
434 |
||
435 |
start_row=4; end_row=22; cols=80; fields_on_screen=end_row+1-start_row; |
|
436 |
||
437 |
*screens=(fields-1)/fields_on_screen+1; |
|
438 |
length= (*screens) * (SC_INFO_LENGTH+ (cols>> 1)+4); |
|
439 |
||
440 |
Create_field *field; |
|
441 |
while ((field=it++)) |
|
442 |
length+=(uint) strlen(field->field_name)+1+TE_INFO_LENGTH+cols/2; |
|
443 |
||
444 |
if (!(info=(uchar*) my_malloc(length,MYF(MY_WME)))) |
|
445 |
DBUG_RETURN(0); |
|
446 |
||
447 |
start_screen=0; |
|
448 |
row=end_row; |
|
449 |
pos=info; |
|
450 |
it.rewind(); |
|
451 |
for (i=0 ; i < fields ; i++) |
|
452 |
{
|
|
453 |
Create_field *cfield=it++; |
|
454 |
if (row++ == end_row) |
|
455 |
{
|
|
456 |
if (i) |
|
457 |
{
|
|
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); |
|
462 |
}
|
|
463 |
row=start_row; |
|
464 |
start_screen=pos; |
|
465 |
pos+=4; |
|
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),' '); |
|
470 |
pos+=(cols >> 1)+4; |
|
471 |
}
|
|
472 |
length=(uint) strlen(cfield->field_name); |
|
473 |
if (length > cols-3) |
|
474 |
length=cols-3; |
|
475 |
||
476 |
if (!small_file) |
|
477 |
{
|
|
478 |
pos[0]=(uchar) row; |
|
479 |
pos[1]=0; |
|
480 |
pos[2]=(uchar) (length+1); |
|
481 |
pos=(uchar*) strmake((char*) pos+3,cfield->field_name,length)+1; |
|
482 |
}
|
|
483 |
cfield->row=(uint8) row; |
|
484 |
cfield->col=(uint8) (length+1); |
|
485 |
cfield->sc_length=(uint8) min(cfield->length,cols-(length+2)); |
|
486 |
}
|
|
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); |
|
491 |
||
492 |
*info_length=(uint) (pos-info); |
|
493 |
DBUG_RETURN(info); |
|
494 |
} /* pack_screens */ |
|
495 |
||
496 |
||
497 |
/* Pack keyinfo and keynames to keybuff for save in form-file. */
|
|
498 |
||
499 |
static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo, |
|
500 |
ulong data_offset) |
|
501 |
{
|
|
502 |
uint key_parts,length; |
|
503 |
uchar *pos, *keyname_pos; |
|
504 |
KEY *key,*end; |
|
505 |
KEY_PART_INFO *key_part,*key_part_end; |
|
506 |
DBUG_ENTER("pack_keys"); |
|
507 |
||
508 |
pos=keybuff+6; |
|
509 |
key_parts=0; |
|
510 |
for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++) |
|
511 |
{
|
|
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); |
|
517 |
pos+=8; |
|
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 ; |
|
524 |
key_part++) |
|
525 |
||
526 |
{
|
|
527 |
uint offset; |
|
528 |
DBUG_PRINT("loop",("field: %d startpos: %lu length: %d", |
|
529 |
key_part->fieldnr, key_part->offset + data_offset, |
|
530 |
key_part->length)); |
|
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); |
|
537 |
pos+=9; |
|
538 |
}
|
|
539 |
}
|
|
540 |
/* Save keynames */
|
|
541 |
keyname_pos=pos; |
|
542 |
*pos++=(uchar) NAMES_SEP_CHAR; |
|
543 |
for (key=keyinfo ; key != end ; key++) |
|
544 |
{
|
|
545 |
uchar *tmp=(uchar*) strmov((char*) pos,key->name); |
|
546 |
*tmp++= (uchar) NAMES_SEP_CHAR; |
|
547 |
*tmp=0; |
|
548 |
pos=tmp; |
|
549 |
}
|
|
550 |
*(pos++)=0; |
|
551 |
||
552 |
for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++) |
|
553 |
{
|
|
554 |
if (key->flags & HA_USES_COMMENT) |
|
555 |
{
|
|
556 |
int2store(pos, key->comment.length); |
|
557 |
uchar *tmp= (uchar*)strnmov((char*) pos+2,key->comment.str,key->comment.length); |
|
558 |
pos= tmp; |
|
559 |
}
|
|
560 |
}
|
|
561 |
||
562 |
if (key_count > 127 || key_parts > 127) |
|
563 |
{
|
|
564 |
keybuff[0]= (key_count & 0x7f) | 0x80; |
|
565 |
keybuff[1]= key_count >> 7; |
|
566 |
int2store(keybuff+2,key_parts); |
|
567 |
}
|
|
568 |
else
|
|
569 |
{
|
|
570 |
keybuff[0]=(uchar) key_count; |
|
571 |
keybuff[1]=(uchar) key_parts; |
|
572 |
keybuff[2]= keybuff[3]= 0; |
|
573 |
}
|
|
574 |
length=(uint) (pos-keyname_pos); |
|
575 |
int2store(keybuff+4,length); |
|
576 |
DBUG_RETURN((uint) (pos-keybuff)); |
|
577 |
} /* pack_keys */ |
|
578 |
||
579 |
||
580 |
/* Make formheader */
|
|
581 |
||
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) |
|
586 |
{
|
|
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"); |
|
591 |
||
592 |
if (create_fields.elements > MAX_FIELDS) |
|
593 |
{
|
|
594 |
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0)); |
|
595 |
DBUG_RETURN(1); |
|
596 |
}
|
|
597 |
||
598 |
totlength= 0L; |
|
599 |
reclength= data_offset; |
|
600 |
no_empty=int_count=int_parts=int_length=time_stamp_pos=null_fields= |
|
601 |
com_length=0; |
|
602 |
n_length=2L; |
|
603 |
||
604 |
/* Check fields */
|
|
605 |
||
606 |
List_iterator<Create_field> it(create_fields); |
|
607 |
Create_field *field; |
|
608 |
while ((field=it++)) |
|
609 |
{
|
|
610 |
uint tmp_len= system_charset_info->cset->charpos(system_charset_info, |
|
611 |
field->comment.str, |
|
612 |
field->comment.str + |
|
613 |
field->comment.length, |
|
614 |
COLUMN_COMMENT_MAXLEN); |
|
615 |
||
616 |
if (tmp_len < field->comment.length) |
|
617 |
{
|
|
618 |
my_error(ER_WRONG_STRING_LENGTH, MYF(0), |
|
619 |
field->comment.str,"COLUMN COMMENT", |
|
620 |
(uint) COLUMN_COMMENT_MAXLEN); |
|
621 |
DBUG_RETURN(1); |
|
622 |
}
|
|
623 |
||
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) |
|
628 |
{
|
|
629 |
field->unireg_check= (Field::utype) ((uint) field->unireg_check | |
|
630 |
MTYP_NOEMPTY_BIT); |
|
631 |
no_empty++; |
|
632 |
}
|
|
633 |
/*
|
|
634 |
We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE
|
|
635 |
as auto-update field.
|
|
636 |
*/
|
|
637 |
if (field->sql_type == MYSQL_TYPE_TIMESTAMP && |
|
638 |
MTYP_TYPENR(field->unireg_check) != Field::NONE && |
|
639 |
!time_stamp_pos) |
|
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; |
|
649 |
if (field->interval) |
|
650 |
{
|
|
651 |
uint old_int_count=int_count; |
|
652 |
||
653 |
if (field->charset->mbminlen > 1) |
|
654 |
{
|
|
655 |
/*
|
|
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.
|
|
662 |
*/
|
|
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); |
|
672 |
||
673 |
for (uint pos= 0; pos < field->interval->count; pos++) |
|
674 |
{
|
|
675 |
char *dst; |
|
676 |
const char *src= field->save_interval->type_names[pos]; |
|
677 |
uint hex_length; |
|
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 + |
|
682 |
1); |
|
683 |
octet2hex(dst, src, length); |
|
684 |
}
|
|
685 |
}
|
|
686 |
||
687 |
field->interval_id=get_interval_id(&int_count,create_fields,field); |
|
688 |
if (old_int_count != int_count) |
|
689 |
{
|
|
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; |
|
693 |
}
|
|
694 |
}
|
|
695 |
if (f_maybe_null(field->pack_flag)) |
|
696 |
null_fields++; |
|
697 |
}
|
|
698 |
int_length+=int_count*2; // 255 prefix + 0 suffix |
|
699 |
||
700 |
/* Save values in forminfo */
|
|
701 |
||
702 |
if (reclength > (ulong) file->max_record_length()) |
|
703 |
{
|
|
704 |
my_error(ER_TOO_BIG_ROWSIZE, MYF(0), (uint) file->max_record_length()); |
|
705 |
DBUG_RETURN(1); |
|
706 |
}
|
|
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) |
|
711 |
{
|
|
712 |
my_message(ER_TOO_MANY_FIELDS, ER(ER_TOO_MANY_FIELDS), MYF(0)); |
|
713 |
DBUG_RETURN(1); |
|
714 |
}
|
|
715 |
||
716 |
bzero((char*)forminfo,288); |
|
717 |
length=(info_length+create_fields.elements*FCOMP+288+n_length+int_length+ |
|
718 |
com_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 */
|
|
736 |
DBUG_RETURN(0); |
|
737 |
} /* pack_header */ |
|
738 |
||
739 |
||
740 |
/* get each unique interval each own id */
|
|
741 |
||
742 |
static uint get_interval_id(uint *int_count,List<Create_field> &create_fields, |
|
743 |
Create_field *last_field) |
|
744 |
{
|
|
745 |
List_iterator<Create_field> it(create_fields); |
|
746 |
Create_field *field; |
|
747 |
TYPELIB *interval=last_field->interval; |
|
748 |
||
749 |
while ((field=it++) != last_field) |
|
750 |
{
|
|
751 |
if (field->interval_id && field->interval->count == interval->count) |
|
752 |
{
|
|
753 |
const char **a,**b; |
|
754 |
for (a=field->interval->type_names, b=interval->type_names ; |
|
755 |
*a && !strcmp(*a,*b); |
|
756 |
a++,b++) ; |
|
757 |
||
758 |
if (! *a) |
|
759 |
{
|
|
760 |
return field->interval_id; // Re-use last interval |
|
761 |
}
|
|
762 |
}
|
|
763 |
}
|
|
764 |
return ++*int_count; // New unique interval |
|
765 |
}
|
|
766 |
||
767 |
||
768 |
/* Save fields, fieldnames and intervals */
|
|
769 |
||
770 |
static bool pack_fields(File file, List<Create_field> &create_fields, |
|
771 |
ulong data_offset) |
|
772 |
{
|
|
773 |
register uint i; |
|
774 |
uint int_count, comment_length=0; |
|
775 |
uchar buff[MAX_FIELD_WIDTH]; |
|
776 |
Create_field *field; |
|
777 |
DBUG_ENTER("pack_fields"); |
|
778 |
||
779 |
/* Write field info */
|
|
780 |
||
781 |
List_iterator<Create_field> it(create_fields); |
|
782 |
||
783 |
int_count=0; |
|
784 |
while ((field=it++)) |
|
785 |
{
|
|
786 |
uint recpos; |
|
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; |
|
798 |
if (field->charset) |
|
799 |
buff[14]= (uchar) field->charset->number; |
|
800 |
else
|
|
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)) |
|
806 |
DBUG_RETURN(1); |
|
807 |
}
|
|
808 |
||
809 |
/* Write fieldnames */
|
|
810 |
buff[0]=(uchar) NAMES_SEP_CHAR; |
|
811 |
if (my_write(file, buff, 1, MYF_RW)) |
|
812 |
DBUG_RETURN(1); |
|
813 |
i=0; |
|
814 |
it.rewind(); |
|
815 |
while ((field=it++)) |
|
816 |
{
|
|
817 |
char *pos= strmov((char*) buff,field->field_name); |
|
818 |
*pos++=NAMES_SEP_CHAR; |
|
819 |
if (i == create_fields.elements-1) |
|
820 |
*pos++=0; |
|
821 |
if (my_write(file, buff, (size_t) (pos-(char*) buff),MYF_RW)) |
|
822 |
DBUG_RETURN(1); |
|
823 |
i++; |
|
824 |
}
|
|
825 |
||
826 |
/* Write intervals */
|
|
827 |
if (int_count) |
|
828 |
{
|
|
829 |
String tmp((char*) buff,sizeof(buff), &my_charset_bin); |
|
830 |
tmp.length(0); |
|
831 |
it.rewind(); |
|
832 |
int_count=0; |
|
833 |
while ((field=it++)) |
|
834 |
{
|
|
835 |
if (field->interval_id > int_count) |
|
836 |
{
|
|
837 |
unsigned char sep= 0; |
|
838 |
unsigned char occ[256]; |
|
839 |
uint i; |
|
840 |
unsigned char *val= NULL; |
|
841 |
||
842 |
bzero(occ, sizeof(occ)); |
|
843 |
||
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; |
|
847 |
||
848 |
if (!occ[(unsigned char)NAMES_SEP_CHAR]) |
|
849 |
sep= (unsigned char) NAMES_SEP_CHAR; |
|
850 |
else if (!occ[(unsigned int)',']) |
|
851 |
sep= ','; |
|
852 |
else
|
|
853 |
{
|
|
854 |
for (uint i=1; i<256; i++) |
|
855 |
{
|
|
856 |
if(!occ[i]) |
|
857 |
{
|
|
858 |
sep= i; |
|
859 |
break; |
|
860 |
}
|
|
861 |
}
|
|
862 |
||
863 |
if(!sep) /* disaster, enum uses all characters, none left as separator */ |
|
864 |
{
|
|
865 |
my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS), |
|
866 |
MYF(0)); |
|
867 |
DBUG_RETURN(1); |
|
868 |
}
|
|
869 |
}
|
|
870 |
||
871 |
int_count= field->interval_id; |
|
872 |
tmp.append(sep); |
|
873 |
for (const char **pos=field->interval->type_names ; *pos ; pos++) |
|
874 |
{
|
|
875 |
tmp.append(*pos); |
|
876 |
tmp.append(sep); |
|
877 |
}
|
|
878 |
tmp.append('\0'); // End of intervall |
|
879 |
}
|
|
880 |
}
|
|
881 |
if (my_write(file,(uchar*) tmp.ptr(),tmp.length(),MYF_RW)) |
|
882 |
DBUG_RETURN(1); |
|
883 |
}
|
|
884 |
if (comment_length) |
|
885 |
{
|
|
886 |
it.rewind(); |
|
887 |
int_count=0; |
|
888 |
while ((field=it++)) |
|
889 |
{
|
|
890 |
if (field->comment.length) |
|
891 |
if (my_write(file, (uchar*) field->comment.str, field->comment.length, |
|
892 |
MYF_RW)) |
|
893 |
DBUG_RETURN(1); |
|
894 |
}
|
|
895 |
}
|
|
896 |
DBUG_RETURN(0); |
|
897 |
}
|
|
898 |
||
899 |
||
900 |
/* save an empty record on start of formfile */
|
|
901 |
||
902 |
static bool make_empty_rec(THD *thd, File file,enum legacy_db_type table_type, |
|
903 |
uint table_options, |
|
904 |
List<Create_field> &create_fields, |
|
905 |
uint reclength, |
|
906 |
ulong data_offset, |
|
907 |
handler *handler) |
|
908 |
{
|
|
909 |
int error= 0; |
|
910 |
Field::utype type; |
|
911 |
uint null_count; |
|
912 |
uchar *buff,*null_pos; |
|
913 |
TABLE table; |
|
914 |
TABLE_SHARE share; |
|
915 |
Create_field *field; |
|
916 |
enum_check_fields old_count_cuted_fields= thd->count_cuted_fields; |
|
917 |
DBUG_ENTER("make_empty_rec"); |
|
918 |
||
919 |
/* We need a table to generate columns for default values */
|
|
920 |
bzero((char*) &table, sizeof(table)); |
|
921 |
bzero((char*) &share, sizeof(share)); |
|
922 |
table.s= &share; |
|
923 |
||
924 |
if (!(buff=(uchar*) my_malloc((size_t) reclength,MYF(MY_WME | MY_ZEROFILL)))) |
|
925 |
{
|
|
926 |
DBUG_RETURN(1); |
|
927 |
}
|
|
928 |
||
929 |
table.in_use= thd; |
|
930 |
table.s->db_low_byte_first= handler->low_byte_first(); |
|
931 |
table.s->blob_ptr_size= portable_sizeof_char_ptr; |
|
932 |
||
933 |
null_count=0; |
|
934 |
if (!(table_options & HA_OPTION_PACK_RECORD)) |
|
935 |
{
|
|
936 |
null_count++; // Need one bit for delete mark |
|
937 |
*buff|= 1; |
|
938 |
}
|
|
939 |
null_pos= buff; |
|
940 |
||
941 |
List_iterator<Create_field> it(create_fields); |
|
942 |
thd->count_cuted_fields= CHECK_FIELD_WARN; // To find wrong default values |
|
943 |
while ((field=it++)) |
|
944 |
{
|
|
945 |
/*
|
|
946 |
regfield don't have to be deleted as it's allocated with sql_alloc()
|
|
947 |
*/
|
|
948 |
Field *regfield= make_field(&share, |
|
949 |
buff+field->offset + data_offset, |
|
950 |
field->length, |
|
951 |
null_pos + null_count / 8, |
|
952 |
null_count & 7, |
|
953 |
field->pack_flag, |
|
954 |
field->sql_type, |
|
955 |
field->charset, |
|
956 |
field->unireg_check, |
|
957 |
field->save_interval ? field->save_interval : |
|
958 |
field->interval, |
|
959 |
field->field_name); |
|
960 |
if (!regfield) |
|
961 |
{
|
|
962 |
error= 1; |
|
963 |
goto err; // End of memory |
|
964 |
}
|
|
965 |
||
966 |
/* save_in_field() will access regfield->table->in_use */
|
|
967 |
regfield->init(&table); |
|
968 |
||
969 |
if (!(field->flags & NOT_NULL_FLAG)) |
|
970 |
{
|
|
971 |
*regfield->null_ptr|= regfield->null_bit; |
|
972 |
null_count++; |
|
973 |
}
|
|
974 |
||
975 |
if (field->sql_type == MYSQL_TYPE_BIT && !f_bit_as_char(field->pack_flag)) |
|
976 |
null_count+= field->length & 7; |
|
977 |
||
978 |
type= (Field::utype) MTYP_TYPENR(field->unireg_check); |
|
979 |
||
980 |
if (field->def) |
|
981 |
{
|
|
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) |
|
985 |
{
|
|
986 |
my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name); |
|
987 |
error= 1; |
|
988 |
delete regfield; //To avoid memory leak |
|
989 |
goto err; |
|
990 |
}
|
|
991 |
}
|
|
992 |
else if (regfield->real_type() == MYSQL_TYPE_ENUM && |
|
993 |
(field->flags & NOT_NULL_FLAG)) |
|
994 |
{
|
|
995 |
regfield->set_notnull(); |
|
996 |
regfield->store((longlong) 1, TRUE); |
|
997 |
}
|
|
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); |
|
1002 |
else
|
|
1003 |
regfield->reset(); |
|
1004 |
}
|
|
1005 |
DBUG_ASSERT(data_offset == ((null_count + 7) / 8)); |
|
1006 |
||
1007 |
/*
|
|
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.
|
|
1010 |
*/
|
|
1011 |
if (null_count & 7) |
|
1012 |
*(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1); |
|
1013 |
||
1014 |
error= my_write(file, buff, (size_t) reclength,MYF_RW) != 0; |
|
1015 |
||
1016 |
err: |
|
1017 |
my_free(buff, MYF(MY_FAE)); |
|
1018 |
thd->count_cuted_fields= old_count_cuted_fields; |
|
1019 |
DBUG_RETURN(error); |
|
1020 |
} /* make_empty_rec */ |