1 /*
2    Copyright (c) 2000, 2011, Oracle and/or its affiliates.
3    Copyright (c) 2009, 2018, MariaDB Corporation
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA
17 */
18 
19 /*
20   Functions to create a unireg form-file from a FIELD and a fieldname-fieldinfo
21   struct.
22   In the following functions FIELD * is an ordinary field-structure with
23   the following exeptions:
24     sc_length,typepos,row,kol,dtype,regnr and field need not to be set.
25     str is a (long) to record position where 0 is the first position.
26 */
27 
28 #include "mariadb.h"
29 #include "sql_priv.h"
30 #include "unireg.h"
31 #include "sql_partition.h"                      // struct partition_info
32 #include "sql_class.h"                  // THD, Internal_error_handler
33 #include "create_options.h"
34 #include "discover.h"
35 #include <m_ctype.h>
36 
37 #define FCOMP			17		/* Bytes for a packed field */
38 
39 /* threshold for safe_alloca */
40 #define ALLOCA_THRESHOLD       2048
41 
42 static uint pack_keys(uchar *,uint, KEY *, ulong);
43 static bool pack_header(THD *, uchar *, List<Create_field> &, HA_CREATE_INFO *,
44                         ulong, handler *);
45 static bool pack_vcols(String *, List<Create_field> &, List<Virtual_column_info> *);
46 static uint get_interval_id(uint *,List<Create_field> &, Create_field *);
47 static bool pack_fields(uchar **, List<Create_field> &, HA_CREATE_INFO*,
48                         ulong);
49 static size_t packed_fields_length(List<Create_field> &);
50 static bool make_empty_rec(THD *, uchar *, uint, List<Create_field> &, uint,
51                            ulong);
52 
53 /*
54   write the length as
55   if (  0 < length <= 255)      one byte
56   if (256 < length <= 65535)    zero byte, then two bytes, low-endian
57 */
extra2_write_len(uchar * pos,size_t len)58 static uchar *extra2_write_len(uchar *pos, size_t len)
59 {
60   if (len <= 255)
61     *pos++= (uchar)len;
62   else
63   {
64     /*
65       At the moment we support options_len up to 64K.
66       We can easily extend it in the future, if the need arises.
67     */
68     DBUG_ASSERT(len <= 65535);
69     int2store(pos + 1, len);
70     pos+= 3;
71   }
72   return pos;
73 }
74 
extra2_write(uchar * pos,enum extra2_frm_value_type type,const LEX_CSTRING * str)75 static uchar *extra2_write(uchar *pos, enum extra2_frm_value_type type,
76                            const LEX_CSTRING *str)
77 {
78   *pos++ = type;
79   pos= extra2_write_len(pos, str->length);
80   memcpy(pos, str->str, str->length);
81   return pos + str->length;
82 }
83 
extra2_write(uchar * pos,enum extra2_frm_value_type type,LEX_CUSTRING * str)84 static uchar *extra2_write(uchar *pos, enum extra2_frm_value_type type,
85                            LEX_CUSTRING *str)
86 {
87   return extra2_write(pos, type, reinterpret_cast<LEX_CSTRING *>(str));
88 }
89 
extra2_write_field_properties(uchar * pos,List<Create_field> & create_fields)90 static uchar *extra2_write_field_properties(uchar *pos,
91                    List<Create_field> &create_fields)
92 {
93   List_iterator<Create_field> it(create_fields);
94   *pos++= EXTRA2_FIELD_FLAGS;
95   /*
96    always first 2  for field visibility
97   */
98   pos= extra2_write_len(pos, create_fields.elements);
99   while (Create_field *cf= it++)
100   {
101     uchar flags= cf->invisible;
102     if (cf->flags & VERS_UPDATE_UNVERSIONED_FLAG)
103       flags|= VERS_OPTIMIZED_UPDATE;
104     *pos++= flags;
105   }
106   return pos;
107 }
108 
109 static const bool ROW_START = true;
110 static const bool ROW_END = false;
111 
112 static inline
113 uint16
vers_get_field(HA_CREATE_INFO * create_info,List<Create_field> & create_fields,bool row_start)114 vers_get_field(HA_CREATE_INFO *create_info, List<Create_field> &create_fields, bool row_start)
115 {
116   DBUG_ASSERT(create_info->versioned());
117 
118   List_iterator<Create_field> it(create_fields);
119   Create_field *sql_field = NULL;
120 
121   const Lex_ident row_field= row_start ? create_info->vers_info.as_row.start
122                                    : create_info->vers_info.as_row.end;
123   DBUG_ASSERT(row_field);
124 
125   for (unsigned field_no = 0; (sql_field = it++); ++field_no)
126   {
127     if (row_field.streq(sql_field->field_name))
128     {
129       DBUG_ASSERT(field_no <= uint16(~0U));
130       return uint16(field_no);
131     }
132   }
133 
134   DBUG_ASSERT(0); /* Not Reachable */
135   return 0;
136 }
137 
138 static inline
has_extra2_field_flags(List<Create_field> & create_fields)139 bool has_extra2_field_flags(List<Create_field> &create_fields)
140 {
141   List_iterator<Create_field> it(create_fields);
142   while (Create_field *f= it++)
143   {
144     if (f->invisible)
145       return true;
146     if (f->flags & VERS_UPDATE_UNVERSIONED_FLAG)
147       return true;
148   }
149   return false;
150 }
151 
152 /**
153   Create a frm (table definition) file
154 
155   @param thd                    Thread handler
156   @param table                  Name of table
157   @param create_info            create info parameters
158   @param create_fields          Fields to create
159   @param keys                   number of keys to create
160   @param key_info               Keys to create
161   @param db_file                Handler to use.
162 
163   @return the generated frm image as a LEX_CUSTRING,
164   or null LEX_CUSTRING (str==0) in case of an error.
165 */
166 
build_frm_image(THD * thd,const LEX_CSTRING * table,HA_CREATE_INFO * create_info,List<Create_field> & create_fields,uint keys,KEY * key_info,handler * db_file)167 LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING *table,
168                               HA_CREATE_INFO *create_info,
169                               List<Create_field> &create_fields,
170                               uint keys, KEY *key_info, handler *db_file)
171 {
172   LEX_CSTRING str_db_type;
173   uint reclength, key_info_length, i;
174   ulong key_buff_length;
175   size_t filepos;
176   ulong data_offset;
177   uint options_len;
178   uint gis_extra2_len= 0;
179   uchar fileinfo[FRM_HEADER_SIZE],forminfo[FRM_FORMINFO_SIZE];
180   const partition_info *part_info= IF_PARTITIONING(thd->work_part_info, 0);
181   bool error;
182   uchar *frm_ptr, *pos;
183   LEX_CUSTRING frm= {0,0};
184   StringBuffer<MAX_FIELD_WIDTH> vcols;
185   DBUG_ENTER("build_frm_image");
186 
187  /* If fixed row records, we need one bit to check for deleted rows */
188   if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
189     create_info->null_bits++;
190   data_offset= (create_info->null_bits + 7) / 8;
191 
192   sql_mode_t save_sql_mode= thd->variables.sql_mode;
193   thd->variables.sql_mode &= ~MODE_ANSI_QUOTES;
194   error= pack_vcols(&vcols, create_fields, create_info->check_constraint_list);
195   thd->variables.sql_mode= save_sql_mode;
196 
197   if (unlikely(error))
198     DBUG_RETURN(frm);
199 
200   if (vcols.length())
201     create_info->expression_length= vcols.length() + FRM_VCOL_NEW_BASE_SIZE;
202 
203   error= pack_header(thd, forminfo, create_fields, create_info,
204                      (ulong)data_offset, db_file);
205   if (unlikely(error))
206     DBUG_RETURN(frm);
207 
208   reclength= uint2korr(forminfo+266);
209 
210   /* Calculate extra data segment length */
211   str_db_type= *hton_name(create_info->db_type);
212   /* str_db_type */
213   create_info->extra_size= (uint)(2 + str_db_type.length +
214                             2 + create_info->connect_string.length);
215   /*
216     Partition:
217       Length of partition info = 4 byte
218       Potential NULL byte at end of partition info string = 1 byte
219       Indicator if auto-partitioned table = 1 byte
220       => Total 6 byte
221   */
222   create_info->extra_size+= 6;
223   if (part_info)
224     create_info->extra_size+= (uint)part_info->part_info_len;
225 
226   for (i= 0; i < keys; i++)
227   {
228     if (key_info[i].parser_name)
229       create_info->extra_size+= (uint)key_info[i].parser_name->length + 1;
230   }
231 
232   options_len= engine_table_options_frm_length(create_info->option_list,
233                                                create_fields,
234                                                keys, key_info);
235 #ifdef HAVE_SPATIAL
236   gis_extra2_len= gis_field_options_image(NULL, create_fields);
237 #endif /*HAVE_SPATIAL*/
238   DBUG_PRINT("info", ("Options length: %u", options_len));
239 
240   if (validate_comment_length(thd, &create_info->comment, TABLE_COMMENT_MAXLEN,
241                               ER_TOO_LONG_TABLE_COMMENT, table->str))
242      DBUG_RETURN(frm);
243   /*
244     If table comment is longer than TABLE_COMMENT_INLINE_MAXLEN bytes,
245     store the comment in an extra segment (up to TABLE_COMMENT_MAXLEN bytes).
246     Pre 5.5, the limit was 60 characters, with no extra segment-handling.
247   */
248   if (create_info->comment.length > TABLE_COMMENT_INLINE_MAXLEN)
249   {
250     forminfo[46]=255;
251     create_info->extra_size+= 2 + (uint)create_info->comment.length;
252   }
253   else
254   {
255     strmake((char*) forminfo+47, create_info->comment.str ?
256             create_info->comment.str : "", create_info->comment.length);
257     forminfo[46]=(uchar) create_info->comment.length;
258   }
259 
260   if (!create_info->tabledef_version.str)
261   {
262     uchar *to= (uchar*) thd->alloc(MY_UUID_SIZE);
263     if (unlikely(!to))
264       DBUG_RETURN(frm);
265     my_uuid(to);
266     create_info->tabledef_version.str= to;
267     create_info->tabledef_version.length= MY_UUID_SIZE;
268   }
269   DBUG_ASSERT(create_info->tabledef_version.length > 0);
270   DBUG_ASSERT(create_info->tabledef_version.length <= 255);
271 
272   prepare_frm_header(thd, reclength, fileinfo, create_info, keys, key_info);
273 
274   /* one byte for a type, one or three for a length */
275   size_t extra2_size= 1 + 1 + create_info->tabledef_version.length;
276   if (options_len)
277     extra2_size+= 1 + (options_len > 255 ? 3 : 1) + options_len;
278 
279   if (part_info)
280     extra2_size+= 1 + 1 + hton_name(part_info->default_engine_type)->length;
281 
282   if (gis_extra2_len)
283     extra2_size+= 1 + (gis_extra2_len > 255 ? 3 : 1) + gis_extra2_len;
284 
285   if (create_info->versioned())
286   {
287     extra2_size+= 1 + 1 + 2 * sizeof(uint16);
288   }
289 
290   bool has_extra2_field_flags_= has_extra2_field_flags(create_fields);
291   if (has_extra2_field_flags_)
292   {
293     extra2_size+= 1 + (create_fields.elements > 255 ? 3 : 1) +
294         create_fields.elements;
295   }
296 
297   key_buff_length= uint4korr(fileinfo+47);
298 
299   frm.length= FRM_HEADER_SIZE;                  // fileinfo;
300   frm.length+= extra2_size + 4;                 // mariadb extra2 frm segment
301 
302   int2store(fileinfo+4, extra2_size);
303   int2store(fileinfo+6, frm.length);            // Position to key information
304   frm.length+= key_buff_length;
305   frm.length+= reclength;                       // row with default values
306   frm.length+= create_info->extra_size;
307 
308   filepos= frm.length;
309   frm.length+= FRM_FORMINFO_SIZE;               // forminfo
310   frm.length+= packed_fields_length(create_fields);
311   frm.length+= create_info->expression_length;
312 
313   if (frm.length > FRM_MAX_SIZE ||
314       create_info->expression_length > UINT_MAX32)
315   {
316     my_error(ER_TABLE_DEFINITION_TOO_BIG, MYF(0), table->str);
317     DBUG_RETURN(frm);
318   }
319 
320   frm_ptr= (uchar*) my_malloc(frm.length, MYF(MY_WME | MY_ZEROFILL |
321                                               MY_THREAD_SPECIFIC));
322   if (!frm_ptr)
323     DBUG_RETURN(frm);
324 
325   /* write the extra2 segment */
326   pos = frm_ptr + 64;
327   compile_time_assert(EXTRA2_TABLEDEF_VERSION != '/');
328   pos= extra2_write(pos, EXTRA2_TABLEDEF_VERSION,
329                     &create_info->tabledef_version);
330 
331   if (part_info)
332     pos= extra2_write(pos, EXTRA2_DEFAULT_PART_ENGINE,
333                       hton_name(part_info->default_engine_type));
334 
335   if (options_len)
336   {
337     *pos++= EXTRA2_ENGINE_TABLEOPTS;
338     pos= extra2_write_len(pos, options_len);
339     pos= engine_table_options_frm_image(pos, create_info->option_list,
340                                         create_fields, keys, key_info);
341   }
342 
343 #ifdef HAVE_SPATIAL
344   if (gis_extra2_len)
345   {
346     *pos= EXTRA2_GIS;
347     pos= extra2_write_len(pos+1, gis_extra2_len);
348     pos+= gis_field_options_image(pos, create_fields);
349   }
350 #endif /*HAVE_SPATIAL*/
351 
352   if (create_info->versioned())
353   {
354     *pos++= EXTRA2_PERIOD_FOR_SYSTEM_TIME;
355     *pos++= 2 * sizeof(uint16);
356     int2store(pos, vers_get_field(create_info, create_fields, ROW_START));
357     pos+= sizeof(uint16);
358     int2store(pos, vers_get_field(create_info, create_fields, ROW_END));
359     pos+= sizeof(uint16);
360   }
361 
362   if (has_extra2_field_flags_)
363     pos= extra2_write_field_properties(pos, create_fields);
364 
365   int4store(pos, filepos); // end of the extra2 segment
366   pos+= 4;
367 
368   DBUG_ASSERT(pos == frm_ptr + uint2korr(fileinfo+6));
369   key_info_length= pack_keys(pos, keys, key_info, data_offset);
370   if (key_info_length > UINT_MAX16)
371   {
372     my_printf_error(ER_CANT_CREATE_TABLE,
373                     "Cannot create table %`s: index information is too long. "
374                     "Decrease number of indexes or use shorter index names or shorter comments.",
375                     MYF(0), table->str);
376     goto err;
377   }
378 
379   int2store(forminfo+2, frm.length - filepos);
380   int4store(fileinfo+10, frm.length);
381   fileinfo[26]= (uchar) MY_TEST((create_info->max_rows == 1) &&
382                                 (create_info->min_rows == 1) && (keys == 0));
383   int2store(fileinfo+28,key_info_length);
384 
385   if (part_info)
386   {
387     fileinfo[61]= (uchar) ha_legacy_type(part_info->default_engine_type);
388     DBUG_PRINT("info", ("part_db_type = %d", fileinfo[61]));
389   }
390 
391   memcpy(frm_ptr, fileinfo, FRM_HEADER_SIZE);
392 
393   pos+= key_buff_length;
394   if (make_empty_rec(thd, pos, create_info->table_options, create_fields,
395                      reclength, data_offset))
396     goto err;
397 
398   pos+= reclength;
399   int2store(pos, create_info->connect_string.length);
400   pos+= 2;
401   if (create_info->connect_string.length)
402     memcpy(pos, create_info->connect_string.str, create_info->connect_string.length);
403   pos+= create_info->connect_string.length;
404   int2store(pos, str_db_type.length);
405   pos+= 2;
406   memcpy(pos, str_db_type.str, str_db_type.length);
407   pos+= str_db_type.length;
408 
409   if (part_info)
410   {
411     char auto_partitioned= part_info->is_auto_partitioned ? 1 : 0;
412     int4store(pos, part_info->part_info_len);
413     pos+= 4;
414     memcpy(pos, part_info->part_info_string, part_info->part_info_len + 1);
415     pos+= part_info->part_info_len + 1;
416     *pos++= auto_partitioned;
417   }
418   else
419   {
420     pos+= 6;
421   }
422 
423   for (i= 0; i < keys; i++)
424   {
425     if (key_info[i].parser_name)
426     {
427       memcpy(pos, key_info[i].parser_name->str, key_info[i].parser_name->length + 1);
428       pos+= key_info[i].parser_name->length + 1;
429     }
430   }
431   if (forminfo[46] == (uchar)255)       // New style MySQL 5.5 table comment
432   {
433     int2store(pos, create_info->comment.length);
434     pos+=2;
435     memcpy(pos, create_info->comment.str, create_info->comment.length);
436     pos+= create_info->comment.length;
437   }
438 
439   memcpy(frm_ptr + filepos, forminfo, FRM_FORMINFO_SIZE);
440   pos= frm_ptr + filepos + FRM_FORMINFO_SIZE;
441   if (pack_fields(&pos, create_fields, create_info, data_offset))
442     goto err;
443 
444   if (vcols.length())
445   {
446     /* Store header for packed fields (extra space for future) */
447     bzero(pos, FRM_VCOL_NEW_BASE_SIZE);
448     pos+= FRM_VCOL_NEW_BASE_SIZE;
449     memcpy(pos, vcols.ptr(), vcols.length());
450     pos+= vcols.length();
451   }
452 
453   {
454     /*
455       Restore all UCS2 intervals.
456       HEX representation of them is not needed anymore.
457     */
458     List_iterator<Create_field> it(create_fields);
459     Create_field *field;
460     while ((field=it++))
461     {
462       if (field->save_interval)
463       {
464         field->interval= field->save_interval;
465         field->save_interval= 0;
466       }
467     }
468   }
469 
470   frm.str= frm_ptr;
471   DBUG_RETURN(frm);
472 
473 err:
474   my_free(frm_ptr);
475   DBUG_RETURN(frm);
476 }
477 
478 
479 /**
480   Create a frm (table definition) file and the tables
481 
482   @param thd           Thread handler
483   @param frm           Binary frm image of the table to create
484   @param path          Name of file (including database, without .frm)
485   @param db            Data base name
486   @param table_name    Table name
487   @param create_info   create info parameters
488   @param file          Handler to use or NULL if only frm needs to be created
489 
490   @retval 0   ok
491   @retval 1   error
492 */
493 
rea_create_table(THD * thd,LEX_CUSTRING * frm,const char * path,const char * db,const char * table_name,HA_CREATE_INFO * create_info,handler * file,bool no_ha_create_table)494 int rea_create_table(THD *thd, LEX_CUSTRING *frm,
495                      const char *path, const char *db, const char *table_name,
496                      HA_CREATE_INFO *create_info, handler *file,
497                      bool no_ha_create_table)
498 {
499   DBUG_ENTER("rea_create_table");
500 
501   if (no_ha_create_table)
502   {
503     if (writefrm(path, db, table_name, true, frm->str, frm->length))
504       goto err_frm;
505   }
506 
507   if (thd->variables.keep_files_on_create)
508     create_info->options|= HA_CREATE_KEEP_FILES;
509 
510   if (file->ha_create_partitioning_metadata(path, NULL, CHF_CREATE_FLAG))
511     goto err_part;
512 
513   if (!no_ha_create_table)
514   {
515     if (ha_create_table(thd, path, db, table_name, create_info, frm))
516       goto err_part;
517   }
518 
519   DBUG_RETURN(0);
520 
521 err_part:
522   file->ha_create_partitioning_metadata(path, NULL, CHF_DELETE_FLAG);
523 err_frm:
524   deletefrm(path);
525   DBUG_RETURN(1);
526 } /* rea_create_table */
527 
528 
529 /* Pack keyinfo and keynames to keybuff for save in form-file. */
530 
pack_keys(uchar * keybuff,uint key_count,KEY * keyinfo,ulong data_offset)531 static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
532                       ulong data_offset)
533 {
534   uint key_parts,length;
535   uchar *pos, *keyname_pos;
536   KEY *key,*end;
537   KEY_PART_INFO *key_part,*key_part_end;
538   DBUG_ENTER("pack_keys");
539 
540   pos=keybuff+6;
541   key_parts=0;
542   for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
543   {
544     int2store(pos, (key->flags ^ HA_NOSAME));
545     int2store(pos+2,key->key_length);
546     pos[4]= (uchar) key->user_defined_key_parts;
547     pos[5]= (uchar) key->algorithm;
548     int2store(pos+6, key->block_size);
549     pos+=8;
550     key_parts+=key->user_defined_key_parts;
551     DBUG_PRINT("loop", ("flags: %lu  key_parts: %d  key_part: %p",
552                         key->flags, key->user_defined_key_parts,
553                         key->key_part));
554     for (key_part=key->key_part,key_part_end=key_part+key->user_defined_key_parts ;
555 	 key_part != key_part_end ;
556 	 key_part++)
557 
558     {
559       uint offset;
560       DBUG_PRINT("loop",("field: %d  startpos: %lu  length: %d",
561 			 key_part->fieldnr, key_part->offset + data_offset,
562                          key_part->length));
563       int2store(pos,key_part->fieldnr+1+FIELD_NAME_USED);
564       offset= (uint) (key_part->offset+data_offset+1);
565       int2store(pos+2, offset);
566       pos[4]=0;					// Sort order
567       int2store(pos+5,key_part->key_type);
568       int2store(pos+7,key_part->length);
569       pos+=9;
570     }
571   }
572 	/* Save keynames */
573   keyname_pos=pos;
574   *pos++=(uchar) NAMES_SEP_CHAR;
575   for (key=keyinfo ; key != end ; key++)
576   {
577     uchar *tmp=(uchar*) strmov((char*) pos,key->name.str);
578     *tmp++= (uchar) NAMES_SEP_CHAR;
579     *tmp=0;
580     pos=tmp;
581   }
582   *(pos++)=0;
583   for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
584   {
585     if (key->flags & HA_USES_COMMENT)
586     {
587       int2store(pos, key->comment.length);
588       uchar *tmp= (uchar*)strnmov((char*) pos+2,key->comment.str,
589                                   key->comment.length);
590       pos= tmp;
591     }
592   }
593 
594   if (key_count > 127 || key_parts > 127)
595   {
596     keybuff[0]= (key_count & 0x7f) | 0x80;
597     keybuff[1]= key_count >> 7;
598     int2store(keybuff+2,key_parts);
599   }
600   else
601   {
602     keybuff[0]=(uchar) key_count;
603     keybuff[1]=(uchar) key_parts;
604     keybuff[2]= keybuff[3]= 0;
605   }
606   length=(uint) (pos-keyname_pos);
607   int2store(keybuff+4,length);
608   DBUG_RETURN((uint) (pos-keybuff));
609 } /* pack_keys */
610 
611 
612 /**
613    Pack the expression (for GENERATED ALWAYS AS, DEFAULT, CHECK)
614 
615    The data is stored as:
616    1 byte      type (enum_vcol_info_type)
617    2 bytes     field_number
618    2 bytes     length of expression
619    1 byte      length of name
620    name
621    next bytes  column expression (text data)
622 
623    @return 0 ok
624    @return 1 error (out of memory or wrong characters in expression)
625 */
626 
pack_expression(String * buf,Virtual_column_info * vcol,uint field_nr,enum_vcol_info_type type)627 static bool pack_expression(String *buf, Virtual_column_info *vcol,
628                             uint field_nr, enum_vcol_info_type type)
629 {
630   if (buf->reserve(FRM_VCOL_NEW_HEADER_SIZE + vcol->name.length))
631     return 1;
632 
633   buf->q_append((char) type);
634   buf->q_append2b(field_nr);
635   size_t len_off= buf->length();
636   buf->q_append2b(0); // to be added later
637   buf->q_append((char)vcol->name.length);
638   buf->q_append(&vcol->name);
639   size_t expr_start= buf->length();
640   vcol->print(buf);
641   size_t expr_len= buf->length() - expr_start;
642   if (expr_len >= 65536)
643   {
644     my_error(ER_EXPRESSION_IS_TOO_BIG, MYF(0), vcol_type_name(type));
645     return 1;
646   }
647   int2store(buf->ptr() + len_off, expr_len);
648   return 0;
649 }
650 
651 
pack_vcols(String * buf,List<Create_field> & create_fields,List<Virtual_column_info> * check_constraint_list)652 static bool pack_vcols(String *buf, List<Create_field> &create_fields,
653                              List<Virtual_column_info> *check_constraint_list)
654 {
655   List_iterator<Create_field> it(create_fields);
656   Create_field *field;
657 
658   for (uint field_nr=0; (field= it++); field_nr++)
659   {
660     if (field->vcol_info)
661       if (pack_expression(buf, field->vcol_info, field_nr,
662                           field->vcol_info->stored_in_db
663                           ? VCOL_GENERATED_STORED : VCOL_GENERATED_VIRTUAL))
664         return 1;
665     if (field->has_default_expression() && !field->has_default_now_unireg_check())
666       if (pack_expression(buf, field->default_value, field_nr, VCOL_DEFAULT))
667         return 1;
668     if (field->check_constraint)
669       if (pack_expression(buf, field->check_constraint, field_nr,
670                           VCOL_CHECK_FIELD))
671         return 1;
672   }
673 
674   List_iterator<Virtual_column_info> cit(*check_constraint_list);
675   Virtual_column_info *check;
676   while ((check= cit++))
677     if (pack_expression(buf, check, UINT_MAX32, VCOL_CHECK_TABLE))
678       return 1;
679   return 0;
680 }
681 
682 
typelib_values_packed_length(const TYPELIB * t)683 static uint typelib_values_packed_length(const TYPELIB *t)
684 {
685   uint length= 0;
686   for (uint i= 0; t->type_names[i]; i++)
687   {
688     length+= t->type_lengths[i];
689     length++; /* Separator */
690   }
691   return length;
692 }
693 
694 
695 /* Make formheader */
696 
pack_header(THD * thd,uchar * forminfo,List<Create_field> & create_fields,HA_CREATE_INFO * create_info,ulong data_offset,handler * file)697 static bool pack_header(THD *thd, uchar *forminfo,
698                         List<Create_field> &create_fields,
699                         HA_CREATE_INFO *create_info, ulong data_offset,
700                         handler *file)
701 {
702   uint int_count,int_length, int_parts;
703   uint time_stamp_pos,null_fields;
704   uint table_options= create_info->table_options;
705   size_t length, reclength, totlength, n_length, com_length;
706   DBUG_ENTER("pack_header");
707 
708   if (create_fields.elements > MAX_FIELDS)
709   {
710     my_message(ER_TOO_MANY_FIELDS, ER_THD(thd, ER_TOO_MANY_FIELDS), MYF(0));
711     DBUG_RETURN(1);
712   }
713 
714   totlength= 0L;
715   reclength= data_offset;
716   int_count=int_parts=int_length=time_stamp_pos=null_fields=0;
717   com_length= 0;
718   n_length=2L;
719   create_info->field_check_constraints= 0;
720 
721   /* Check fields */
722   List_iterator<Create_field> it(create_fields);
723   Create_field *field;
724   while ((field=it++))
725   {
726     if (validate_comment_length(thd, &field->comment, COLUMN_COMMENT_MAXLEN,
727                                 ER_TOO_LONG_FIELD_COMMENT,
728                                 field->field_name.str))
729        DBUG_RETURN(1);
730 
731     totlength+= (size_t)field->length;
732     com_length+= field->comment.length;
733     /*
734       We mark first TIMESTAMP field with NOW() in DEFAULT or ON UPDATE
735       as auto-update field.
736     */
737     if (field->real_field_type() == MYSQL_TYPE_TIMESTAMP &&
738         MTYP_TYPENR(field->unireg_check) != Field::NONE &&
739 	!time_stamp_pos)
740       time_stamp_pos= (uint) field->offset+ (uint) data_offset + 1;
741     length=field->pack_length;
742     if ((uint) field->offset+ (uint) data_offset+ length > reclength)
743       reclength=(uint) (field->offset+ data_offset + length);
744     n_length+= field->field_name.length + 1;
745     field->interval_id=0;
746     field->save_interval= 0;
747     if (field->interval)
748     {
749       uint old_int_count=int_count;
750 
751       if (field->charset->mbminlen > 1)
752       {
753         /*
754           Escape UCS2 intervals using HEX notation to avoid
755           problems with delimiters between enum elements.
756           As the original representation is still needed in
757           the function make_empty_rec to create a record of
758           filled with default values it is saved in save_interval
759           The HEX representation is created from this copy.
760         */
761         field->save_interval= field->interval;
762         field->interval= (TYPELIB*) thd->alloc(sizeof(TYPELIB));
763         *field->interval= *field->save_interval;
764         field->interval->type_names=
765           (const char **) thd->alloc(sizeof(char*) *
766                                      (field->interval->count+1));
767         field->interval->type_names[field->interval->count]= 0;
768         field->interval->type_lengths=
769           (uint *) thd->alloc(sizeof(uint) * field->interval->count);
770 
771         for (uint pos= 0; pos < field->interval->count; pos++)
772         {
773           char *dst;
774           const char *src= field->save_interval->type_names[pos];
775           size_t hex_length;
776           length= field->save_interval->type_lengths[pos];
777           hex_length= length * 2;
778           field->interval->type_lengths[pos]= (uint)hex_length;
779           field->interval->type_names[pos]= dst=
780             (char*) thd->alloc(hex_length + 1);
781           octet2hex(dst, src, length);
782         }
783       }
784 
785       field->interval_id=get_interval_id(&int_count,create_fields,field);
786       if (old_int_count != int_count)
787       {
788         int_length+= typelib_values_packed_length(field->interval);
789         int_parts+= field->interval->count + 1;
790       }
791     }
792     if (f_maybe_null(field->pack_flag))
793       null_fields++;
794     if (field->check_constraint)
795       create_info->field_check_constraints++;
796   }
797   int_length+=int_count*2;			// 255 prefix + 0 suffix
798 
799   /* Save values in forminfo */
800   if (reclength > (ulong) file->max_record_length())
801   {
802     my_error(ER_TOO_BIG_ROWSIZE, MYF(0), static_cast<long>(file->max_record_length()));
803     DBUG_RETURN(1);
804   }
805 
806   /* Hack to avoid bugs with small static rows in MySQL */
807   reclength= MY_MAX(file->min_record_length(table_options), reclength);
808   length= n_length + create_fields.elements*FCOMP + FRM_FORMINFO_SIZE +
809           int_length + com_length + create_info->expression_length;
810   if (length > 65535L || int_count > 255)
811   {
812     my_message(ER_TOO_MANY_FIELDS, "Table definition is too large", MYF(0));
813     DBUG_RETURN(1);
814   }
815 
816   bzero((char*)forminfo,FRM_FORMINFO_SIZE);
817   int2store(forminfo,length);
818   int2store(forminfo+258,create_fields.elements);
819   // bytes 260-261 are unused
820   int2store(forminfo+262,totlength);
821   // bytes 264-265 are unused
822   int2store(forminfo+266,reclength);
823   int2store(forminfo+268,n_length);
824   int2store(forminfo+270,int_count);
825   int2store(forminfo+272,int_parts);
826   int2store(forminfo+274,int_length);
827   int2store(forminfo+276,time_stamp_pos);
828   int2store(forminfo+278,80);			/* Columns needed */
829   int2store(forminfo+280,22);			/* Rows needed */
830   int2store(forminfo+282,null_fields);
831   int2store(forminfo+284,com_length);
832   int2store(forminfo+286,create_info->expression_length);
833   DBUG_RETURN(0);
834 } /* pack_header */
835 
836 
837 /* get each unique interval each own id */
get_interval_id(uint * int_count,List<Create_field> & create_fields,Create_field * last_field)838 static uint get_interval_id(uint *int_count,List<Create_field> &create_fields,
839 			    Create_field *last_field)
840 {
841   List_iterator<Create_field> it(create_fields);
842   Create_field *field;
843   TYPELIB *interval=last_field->interval;
844 
845   while ((field=it++) != last_field)
846   {
847     if (field->interval_id && field->interval->count == interval->count)
848     {
849       const char **a,**b;
850       for (a=field->interval->type_names, b=interval->type_names ;
851 	   *a && !strcmp(*a,*b);
852 	   a++,b++) ;
853 
854       if (! *a)
855       {
856 	return field->interval_id;		// Re-use last interval
857       }
858     }
859   }
860   return ++*int_count;				// New unique interval
861 }
862 
863 
packed_fields_length(List<Create_field> & create_fields)864 static size_t packed_fields_length(List<Create_field> &create_fields)
865 {
866   Create_field *field;
867   size_t length= 0;
868   DBUG_ENTER("packed_fields_length");
869 
870   List_iterator<Create_field> it(create_fields);
871   uint int_count=0;
872   while ((field=it++))
873   {
874     if (field->interval_id > int_count)
875     {
876       int_count= field->interval_id;
877       length++;
878       length+= typelib_values_packed_length(field->interval);
879       length++;
880     }
881 
882     length+= FCOMP;
883     length+= field->field_name.length + 1;
884     length+= field->comment.length;
885   }
886   length+= 2;
887   DBUG_RETURN(length);
888 }
889 
890 /* Save fields, fieldnames and intervals */
891 
pack_fields(uchar ** buff_arg,List<Create_field> & create_fields,HA_CREATE_INFO * create_info,ulong data_offset)892 static bool pack_fields(uchar **buff_arg, List<Create_field> &create_fields,
893                         HA_CREATE_INFO *create_info,
894                         ulong data_offset)
895 {
896   uchar *buff= *buff_arg;
897   uint int_count;
898   size_t comment_length= 0;
899   Create_field *field;
900   DBUG_ENTER("pack_fields");
901 
902   /* Write field info */
903   List_iterator<Create_field> it(create_fields);
904   int_count=0;
905   while ((field=it++))
906   {
907     uint recpos;
908     int2store(buff+3, field->length);
909     /* The +1 is here becasue the col offset in .frm file have offset 1 */
910     recpos= field->offset+1 + (uint) data_offset;
911     int3store(buff+5,recpos);
912     int2store(buff+8,field->pack_flag);
913     buff[10]= (uchar) field->unireg_check;
914     buff[12]= (uchar) field->interval_id;
915     buff[13]= (uchar) field->real_field_type();
916     if (field->real_field_type() == MYSQL_TYPE_GEOMETRY)
917     {
918       buff[11]= 0;
919       buff[14]= (uchar) field->geom_type;
920 #ifndef HAVE_SPATIAL
921       DBUG_ASSERT(0);                           // Should newer happen
922 #endif
923     }
924     else if (field->charset)
925     {
926       buff[11]= (uchar) (field->charset->number >> 8);
927       buff[14]= (uchar) field->charset->number;
928     }
929     else
930     {
931       buff[11]= buff[14]= 0;			// Numerical
932     }
933 
934     int2store(buff+15, field->comment.length);
935     comment_length+= field->comment.length;
936     set_if_bigger(int_count,field->interval_id);
937     buff+= FCOMP;
938   }
939 
940   /* Write fieldnames */
941   *buff++= NAMES_SEP_CHAR;
942   it.rewind();
943   while ((field=it++))
944   {
945     buff= (uchar*)strmov((char*) buff, field->field_name.str);
946     *buff++=NAMES_SEP_CHAR;
947   }
948   *buff++= 0;
949 
950   /* Write intervals */
951   if (int_count)
952   {
953     it.rewind();
954     int_count=0;
955     while ((field=it++))
956     {
957       if (field->interval_id > int_count)
958       {
959         unsigned char  sep= 0;
960         unsigned char  occ[256];
961         uint           i;
962         unsigned char *val= NULL;
963 
964         bzero(occ, sizeof(occ));
965 
966         for (i=0; (val= (unsigned char*) field->interval->type_names[i]); i++)
967           for (uint j = 0; j < field->interval->type_lengths[i]; j++)
968             occ[(unsigned int) (val[j])]= 1;
969 
970         if (!occ[(unsigned char)NAMES_SEP_CHAR])
971           sep= (unsigned char) NAMES_SEP_CHAR;
972         else if (!occ[(unsigned int)','])
973           sep= ',';
974         else
975         {
976           for (uint i=1; i<256; i++)
977           {
978             if(!occ[i])
979             {
980               sep= i;
981               break;
982             }
983           }
984 
985           if (!sep)
986           {
987             /* disaster, enum uses all characters, none left as separator */
988             my_message(ER_WRONG_FIELD_TERMINATORS,
989                        ER(ER_WRONG_FIELD_TERMINATORS),
990                        MYF(0));
991             DBUG_RETURN(1);
992           }
993         }
994 
995         int_count= field->interval_id;
996         *buff++= sep;
997         for (int i=0; field->interval->type_names[i]; i++)
998         {
999           memcpy(buff, field->interval->type_names[i], field->interval->type_lengths[i]);
1000           buff+= field->interval->type_lengths[i];
1001           *buff++= sep;
1002         }
1003         *buff++= 0;
1004       }
1005     }
1006   }
1007   if (comment_length)
1008   {
1009     it.rewind();
1010     while ((field=it++))
1011     {
1012       if (size_t l= field->comment.length)
1013       {
1014         memcpy(buff, field->comment.str, l);
1015         buff+= l;
1016       }
1017     }
1018   }
1019   *buff_arg= buff;
1020   DBUG_RETURN(0);
1021 }
1022 
1023 /* save an empty record on start of formfile */
1024 
make_empty_rec(THD * thd,uchar * buff,uint table_options,List<Create_field> & create_fields,uint reclength,ulong data_offset)1025 static bool make_empty_rec(THD *thd, uchar *buff, uint table_options,
1026 			   List<Create_field> &create_fields,
1027 			   uint reclength, ulong data_offset)
1028 {
1029   int error= 0;
1030   uint null_count;
1031   uchar *null_pos;
1032   TABLE table;
1033   TABLE_SHARE share;
1034   Create_field *field;
1035   Check_level_instant_set old_count_cuted_fields(thd, CHECK_FIELD_WARN);
1036   Abort_on_warning_instant_set old_abort_on_warning(thd, 0);
1037   DBUG_ENTER("make_empty_rec");
1038 
1039   /* We need a table to generate columns for default values */
1040   bzero((char*) &table, sizeof(table));
1041   bzero((char*) &share, sizeof(share));
1042   table.s= &share;
1043 
1044   table.in_use= thd;
1045 
1046   null_count=0;
1047   if (!(table_options & HA_OPTION_PACK_RECORD))
1048   {
1049     null_count++;			// Need one bit for delete mark
1050     *buff|= 1;
1051   }
1052   null_pos= buff;
1053 
1054   List_iterator<Create_field> it(create_fields);
1055   while ((field=it++))
1056   {
1057     /* regfield don't have to be deleted as it's allocated on THD::mem_root */
1058     Field *regfield= make_field(&share, thd->mem_root,
1059                                 buff+field->offset + data_offset,
1060                                 (uint32)field->length,
1061                                 null_pos + null_count / 8,
1062                                 null_count & 7,
1063                                 field->pack_flag,
1064                                 field->type_handler(),
1065                                 field->charset,
1066                                 field->geom_type, field->srid,
1067                                 field->unireg_check,
1068                                 field->save_interval ? field->save_interval
1069                                                      : field->interval,
1070                                 &field->field_name,
1071                                 field->flags);
1072     if (!regfield)
1073     {
1074       error= 1;
1075       goto err;                                 // End of memory
1076     }
1077 
1078     /* save_in_field() will access regfield->table->in_use */
1079     regfield->init(&table);
1080 
1081     if (!(field->flags & NOT_NULL_FLAG))
1082     {
1083       *regfield->null_ptr|= regfield->null_bit;
1084       null_count++;
1085     }
1086 
1087     if (field->real_field_type() == MYSQL_TYPE_BIT &&
1088         !f_bit_as_char(field->pack_flag))
1089       null_count+= field->length & 7;
1090 
1091     if (field->default_value && !field->default_value->flags &&
1092         !field->vers_sys_field() &&
1093         (!(field->flags & BLOB_FLAG) ||
1094          field->real_field_type() == MYSQL_TYPE_GEOMETRY))
1095     {
1096       Item *expr= field->default_value->expr;
1097       // may be already fixed if ALTER TABLE
1098       int res= expr->fix_fields_if_needed(thd, &expr);
1099       if (!res)
1100         res= expr->save_in_field(regfield, 1);
1101       if (!res && (field->flags & BLOB_FLAG))
1102         regfield->reset();
1103 
1104       /* If not ok or warning of level 'note' */
1105       if (res != 0 && res != 3)
1106       {
1107         my_error(ER_INVALID_DEFAULT, MYF(0), regfield->field_name.str);
1108         error= 1;
1109         delete regfield; //To avoid memory leak
1110         goto err;
1111       }
1112       delete regfield; //To avoid memory leak
1113     }
1114     else if (regfield->real_type() == MYSQL_TYPE_ENUM &&
1115              !field->vers_sys_field() &&
1116 	     (field->flags & NOT_NULL_FLAG))
1117     {
1118       regfield->set_notnull();
1119       regfield->store((longlong) 1, TRUE);
1120     }
1121     else
1122       regfield->reset();
1123   }
1124   DBUG_ASSERT(data_offset == ((null_count + 7) / 8));
1125 
1126   /*
1127     We need to set the unused bits to 1. If the number of bits is a multiple
1128     of 8 there are no unused bits.
1129   */
1130   if (null_count & 7)
1131     *(null_pos + null_count / 8)|= ~(((uchar) 1 << (null_count & 7)) - 1);
1132 
1133 err:
1134   DBUG_RETURN(error);
1135 } /* make_empty_rec */
1136