http://www.cnblogs.com/jiangxu67/p/4755097.html

http://www.cnblogs.com/jiangxu67/p/4755097.html

http://guduwhuzhe.iteye.com/blog/1887619

  1. bool mysql_create_frm(THD *thd, const char *file_name,
  2. const char *db, const char *table,
  3. HA_CREATE_INFO *create_info,
  4. List<Create_field> &create_fields,
  5. uint keys, KEY *key_info,
  6. handler *db_file)
  7. {
  8. LEX_STRING str_db_type;
  9. uint reclength, info_length, screens, key_info_length, maxlength, tmp_len, i;
  10. ulong key_buff_length;
  11. File file;
  12. ulong filepos, data_offset;
  13. uchar fileinfo[],forminfo[],*keybuff;
  14. uchar *screen_buff;
  15. ];
  16.  
  17. Pack_header_error_handler pack_header_error_handler;
  18. int error;
  19. ;
  20. uint format_section_length;
  21. ;
  22. DBUG_ENTER("mysql_create_frm");
  23.  
  24. DBUG_ASSERT(*fn_rext((char*)file_name)); // Check .frm extension
  25.  
  26. )))
  27. DBUG_RETURN();
  28. DBUG_ASSERT(db_file != NULL);
  29.  
  30. /* If fixed row records, we need one bit to check for deleted rows */
  31. if (!(create_info->table_options & HA_OPTION_PACK_RECORD))
  32. create_info->null_bits++;
  33. data_offset= (create_info->null_bits + ) / ;
  34.  
  35. thd->push_internal_handler(&pack_header_error_handler);
  36.  
  37. error= pack_header(forminfo, ha_legacy_type(create_info->db_type),
  38. create_fields,info_length,
  39. screens, create_info->table_options,
  40. data_offset, db_file);
  41.  
  42. thd->pop_internal_handler();
  43.  
  44. if (error)
  45. {
  46. my_free(screen_buff);
  47.  
  48. }
  49. reclength=uint2korr(forminfo+);
  50.  
  51. /* Calculate extra data segment length */
  52. str_db_type.str= (char *) ha_resolve_storage_engine_name(create_info->db_type);
  53. str_db_type.length= strlen(str_db_type.str);
  54. /* str_db_type */
  55. create_info->extra_size= ( + str_db_type.length +
  56. + create_info->connect_string.length);
  57.  
  58. create_info->extra_size+= ;
  59.  
  60. /*
  61. If table comment is longer than TABLE_COMMENT_INLINE_MAXLEN bytes,
  62. store the comment in an extra segment (up to TABLE_COMMENT_MAXLEN bytes).
  63. Pre 6.0, the limit was 60 characters, with no extra segment-handling.
  64. */
  65. if (create_info->comment.length > TABLE_COMMENT_INLINE_MAXLEN)
  66. {
  67. forminfo[]=;
  68. create_info->extra_size+= + create_info->comment.length;
  69. }
  70. else{
  71. strmake((, create_info->comment.str ?
  72. create_info->comment.str : "", create_info->comment.length);
  73. forminfo[]=(uchar) create_info->comment.length;
  74. }
  75.  
  76. if ((file=create_frm(thd, file_name, db, table, reclength, fileinfo,
  77. create_info, keys, key_info)) < )
  78. {
  79. my_free(screen_buff);
  80. DBUG_RETURN();
  81. }
  82.  
  83. key_buff_length= uint4korr(fileinfo+);
  84. keybuff=(uchar*) my_malloc(key_buff_length, MYF());
  85. key_info_length= pack_keys(keybuff, keys, key_info, data_offset);
  86.  
  87. /*
  88. Ensure that there are no forms in this newly created form file.
  89. Even if the form file exists, create_frm must truncate it to
  90. ensure one form per form file.
  91. */
  92. DBUG_ASSERT(uint2korr(fileinfo+) == );
  93.  
  94. if (!(filepos= make_new_entry(file, fileinfo, NULL, "")))
  95. goto err;
  96. maxlength=());
  97. int2store(forminfo+,maxlength);
  98. int4store(fileinfo+,(ulong) (filepos+maxlength));
  99. fileinfo[]= (uchar) test((create_info->max_rows == ) &&
  100. (create_info->min_rows == ) && (keys == ));
  101. int2store(fileinfo+,key_info_length);
  102.  
  103. int2store(fileinfo+,db_file->extra_rec_buf_length());
  104.  
  105. , 0L, MYF_RW) ||
  106. mysql_file_pwrite(file, keybuff, key_info_length,
  107. (), MYF_RW))
  108. goto err;
  109. mysql_file_seek(file,
  110. () + (ulong) key_buff_length,
  111. MY_SEEK_SET, MYF());
  112. if (make_empty_rec(thd,file,ha_legacy_type(create_info->db_type),
  113. create_info->table_options,
  114. create_fields,reclength, data_offset, db_file))
  115. goto err;
  116.  
  117. int2store(buff, create_info->connect_string.length);
  118. , MYF(MY_NABP)) ||
  119. mysql_file_write(file, (const uchar*)create_info->connect_string.str,
  120. create_info->connect_string.length, MYF(MY_NABP)))
  121. goto err;
  122.  
  123. int2store(buff, str_db_type.length);
  124. , MYF(MY_NABP)) ||
  125. mysql_file_write(file, (const uchar*)str_db_type.str,
  126. str_db_type.length, MYF(MY_NABP)))
  127. goto err;
  128.  
  129. {
  130. bzero((uchar*) buff, );
  131. , MYF_RW))
  132. goto err;
  133. }
  134. ; i < keys; i++)
  135. {
  136. if (key_info[i].parser_name)
  137. {
  138. if (mysql_file_write(file, (const uchar*)key_info[i].parser_name->str,
  139. key_info[i].parser_name->length + , MYF(MY_NABP)))
  140. goto err;
  141. }
  142. }
  143. ] == (uchar))
  144. {
  145. uchar comment_length_buff[];
  146. int2store(comment_length_buff,create_info->comment.length);
  147. , MYF(MY_NABP)) ||
  148. mysql_file_write(file, (uchar*) create_info->comment.str,
  149. create_info->comment.length, MYF(MY_NABP)))
  150. goto err;
  151. }
  152.  
  153. /* "Format section" with additional table and column properties */
  154.  
  155. mysql_file_seek(file, filepos, MY_SEEK_SET, MYF());
  156. , MYF_RW) ||
  157. mysql_file_write(file, screen_buff, info_length, MYF_RW) ||
  158. pack_fields(file, create_fields, data_offset))
  159. goto err;
  160.  
  161. my_free(screen_buff);
  162. my_free(keybuff);
  163.  
  164. if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
  165. (mysql_file_sync(file, MYF(MY_WME)) ||
  166. my_sync_dir_by_file(file_name, MYF(MY_WME))))
  167. goto err2;
  168.  
  169. if (mysql_file_close(file, MYF(MY_WME)))
  170. goto err3;
  171.  
  172. err:
  173. my_free(screen_buff);
  174. my_free(keybuff);
  175. err2:
  176. (void) mysql_file_close(file, MYF(MY_WME));
  177. err3:
  178. mysql_file_delete(key_file_frm, file_name, MYF());
  179. DBUG_RETURN();
  180. } /* mysql_create_frm */
  1. /* Pack keyinfo and keynames to keybuff for save in form-file. */
  2.  
  3. static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo,
  4. ulong data_offset)
  5. {
  6. uint key_parts,length;
  7. uchar *pos, *keyname_pos;
  8. KEY *key,*end;
  9. KEY_PART_INFO *key_part,*key_part_end;
  10. DBUG_ENTER("pack_keys");
  11.  
  12. pos=keybuff+;
  13. key_parts=;
  14. for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
  15. {
  16. int2store(pos, (key->flags ^ HA_NOSAME));
  17. int2store(pos+,key->key_length);
  18. pos[]= (uchar) key->key_parts;
  19. pos[]= (uchar) key->algorithm;
  20. int2store(pos+, key->block_size);
  21. pos+=;
  22. key_parts+=key->key_parts;
  23. DBUG_PRINT("loop", ("flags: %lu key_parts: %d at 0x%lx",
  24. key->flags, key->key_parts,
  25. (long) key->key_part));
  26. for (key_part=key->key_part,key_part_end=key_part+key->key_parts ;
  27. key_part != key_part_end ;
  28. key_part++)
  29.  
  30. {
  31. uint offset;
  32. DBUG_PRINT("loop",("field: %d startpos: %lu length: %d",
  33. key_part->fieldnr, key_part->offset + data_offset,
  34. key_part->length));
  35. int2store(pos,key_part->fieldnr++FIELD_NAME_USED);
  36. offset= ();
  37. int2store(pos+, offset);
  38. pos[]=; // Sort order
  39. int2store(pos+,key_part->key_type);
  40. int2store(pos+,key_part->length);
  41. pos+=;
  42. }
  43. }
  44. /* Save keynames */
  45. keyname_pos=pos;
  46. *pos++=(uchar) NAMES_SEP_CHAR;
  47. for (key=keyinfo ; key != end ; key++)
  48. {
  49. uchar *tmp=(uchar*) strmov((char*) pos,key->name);
  50. *tmp++= (uchar) NAMES_SEP_CHAR;
  51. *tmp=;
  52. pos=tmp;
  53. }
  54. *(pos++)=;
  55. for (key=keyinfo,end=keyinfo+key_count ; key != end ; key++)
  56. {
  57. if (key->flags & HA_USES_COMMENT)
  58. {
  59. int2store(pos, key->comment.length);
  60. uchar *tmp= (uchar*)strnmov((,key->comment.str,
  61. key->comment.length);
  62. pos= tmp;
  63. }
  64. }
  65.  
  66. || key_parts > )
  67. {
  68. keybuff[]= (key_count & 0x7f) | 0x80;
  69. keybuff[]= key_count >> ;
  70. int2store(keybuff+,key_parts);
  71. }
  72. else
  73. {
  74. keybuff[]=(uchar) key_count;
  75. keybuff[]=(uchar) key_parts;
  76. keybuff[]= keybuff[]= ;
  77. }
  78. length=(uint) (pos-keyname_pos);
  79. int2store(keybuff+,length);
  80. DBUG_RETURN((uint) (pos-keybuff));
  81. } /* pack_keys */
  1. /* Save fields, fieldnames and intervals */
  2.  
  3. static bool pack_fields(File file, List<Create_field> &create_fields,
  4. ulong data_offset)
  5. {
  6. reg2 uint i;
  7. ;
  8. uchar buff[MAX_FIELD_WIDTH];
  9. Create_field *field;
  10. DBUG_ENTER("pack_fields");
  11.  
  12. /* Write field info */
  13.  
  14. List_iterator<Create_field> it(create_fields);
  15.  
  16. int_count=;
  17. while ((field=it++))
  18. {
  19. uint recpos;
  20. buff[]= (uchar) field->row;
  21. buff[]= (uchar) field->col;
  22. buff[]= (uchar) field->sc_length;
  23. int2store(buff+, field->length);
  24. /* The +1 is here becasue the col offset in .frm file have offset 1 */
  25. recpos= field->offset+ + (uint) data_offset;
  26. int3store(buff+,recpos);
  27. int2store(buff+,field->pack_flag);
  28. DBUG_ASSERT(field->unireg_check < );
  29. buff[]= (uchar) field->unireg_check;
  30. buff[]= (uchar) field->interval_id;
  31. buff[]= (uchar) field->sql_type;
  32. if (field->sql_type == MYSQL_TYPE_GEOMETRY)
  33. {
  34. buff[]= ;
  35. buff[]= (uchar) field->geom_type;
  36. #ifndef HAVE_SPATIAL
  37. DBUG_ASSERT(); // Should newer happen
  38. #endif
  39. }
  40. else if (field->charset)
  41. {
  42. buff[]= (uchar) (field->charset->number >> );
  43. buff[]= (uchar) field->charset->number;
  44. }
  45. else
  46. {
  47. buff[]= buff[]= ; // Numerical
  48. }
  49. int2store(buff+, field->comment.length);
  50. comment_length+= field->comment.length;
  51. set_if_bigger(int_count,field->interval_id);
  52. if (mysql_file_write(file, buff, FCOMP, MYF_RW))
  53. DBUG_RETURN();
  54. }
  55.  
  56. /* Write fieldnames */
  57. buff[]=(uchar) NAMES_SEP_CHAR;
  58. , MYF_RW))
  59. DBUG_RETURN();
  60. i=;
  61. it.rewind();
  62. while ((field=it++))
  63. {
  64. char *pos= strmov((char*) buff,field->field_name);
  65. *pos++=NAMES_SEP_CHAR;
  66. )
  67. *pos++=;
  68. if (mysql_file_write(file, buff, (size_t) (pos-(char*) buff), MYF_RW))
  69. DBUG_RETURN();
  70. i++;
  71. }
  72.  
  73. /* Write intervals */
  74. if (int_count)
  75. {
  76. String tmp((char*) buff,sizeof(buff), &my_charset_bin);
  77. tmp.length();
  78. it.rewind();
  79. int_count=;
  80. while ((field=it++))
  81. {
  82. if (field->interval_id > int_count)
  83. {
  84. unsigned ;
  85. unsigned ];
  86. uint i;
  87. unsigned char *val= NULL;
  88.  
  89. bzero(occ, sizeof(occ));
  90.  
  91. ; (val= (unsigned char*) field->interval->type_names[i]); i++)
  92. ; j < field->interval->type_lengths[i]; j++)
  93. occ[(unsigned ;
  94.  
  95. if (!occ[(unsigned char)NAMES_SEP_CHAR])
  96. sep= (unsigned char) NAMES_SEP_CHAR;
  97. else if (!occ[(unsigned int)','])
  98. sep= ',';
  99. else
  100. {
  101. ; i<; i++)
  102. {
  103. if(!occ[i])
  104. {
  105. sep= i;
  106. break;
  107. }
  108. }
  109.  
  110. if(!sep) /* disaster, enum uses all characters, none left as separator */
  111. {
  112. my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
  113. MYF());
  114. DBUG_RETURN();
  115. }
  116. }
  117.  
  118. int_count= field->interval_id;
  119. tmp.append(sep);
  120. for (const char **pos=field->interval->type_names ; *pos ; pos++)
  121. {
  122. tmp.append(*pos);
  123. tmp.append(sep);
  124. }
  125. tmp.append('\0'); // End of intervall
  126. }
  127. }
  128. if (mysql_file_write(file, (uchar*) tmp.ptr(), tmp.length(), MYF_RW))
  129. DBUG_RETURN();
  130. }
  131. if (comment_length)
  132. {
  133. it.rewind();
  134. int_count=;
  135. while ((field=it++))
  136. {
  137. if (field->comment.length)
  138. if (mysql_file_write(file, (uchar*) field->comment.str,
  139. field->comment.length, MYF_RW))
  140. DBUG_RETURN();
  141. }
  142. }
  143. DBUG_RETURN();
  144. }

mysql_create_frm的更多相关文章

  1. 【MySQL】frm文件解析

    官网说明:http://dev.mysql.com/doc/internals/en/frm-file-format.html frm是MySQL表结构定义文件,通常frm文件是不会损坏的,但是如果出 ...

  2. online ddl 跟踪

    | >mysql_prepare_alter_table T@4 : | | | | | | <mysql_prepare_alter_table 7368 T@4 : | | | | | ...

随机推荐

  1. Team Homework #1 学长“学霸英语学习软件”试用

    简介: 一款英语单词记忆和管理辅助软件. 基本功能: 内置GRE词汇及其常考形态.Webster英语解释 单词发音功能 单词测验模式 简易词典功能 基本界面 词库单词读取 单词测试 优点: 1.界面简 ...

  2. Java 8 VM GC Tuning Guide Charter2

    第二章 Ergonomics Ergonomics is the process by which the Java Virtual Machine (JVM) and garbage collect ...

  3. android 开发自建wifi热点的默认ip

    android 开发自建wifi热点的默认ip是:192.168.43.1  (小米3测试)

  4. unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor

    eclipse启动项目时,提示超时: 解决方案: 修改 workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml文件.  ...

  5. angular事件代理

    在angular中,是不支持事件代理的,有些时候,我们需要处理比较多的数据,尤其是一些列表的时候,可能会很多,如果给每一项都加事件的话,注定慢很多,为了解决这个事情,因此需要一个做事件代理的direc ...

  6. Memcached 安装及配置

    下载Memcached.exe 保存到c:\memcached 运行command: 输入 c:\memcached\memcached.exe -d install 回车,安装memcached s ...

  7. PHP对XML添加节点之appendChild()方法讲解

    问题如下:<b > <c>test</c> </b>我要在b节点里面添加一个子节点比如说加一个d节点,要实现成<b > <c>t ...

  8. xml存储图片 二进制存储图片

    一.保存图片到XML文件 /// <summary> /// 保存图片到XML文件 /// </summary> private void UploadImageToXml() ...

  9. model对象之setter方法使用,解决去除空格和将数字转成字符串展示方法

    1.系统中手机号注册的时候,不能含有前后空格.在model对象中过滤~! private String mobile; public String getMobile() { return mobil ...

  10. lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上

    题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...