字段
结构
innodb 使用dict_col_t表示字段属性,用dfield_t在进行sql操作时表示row record中的字段。
dict_col_t
/** Data structure for a column in a table */
struct dict_col_t{
/*----------------------*/
/** The following are copied from dtype_t,
so that all bit-fields can be packed tightly. */
/* @{ */
unsigned prtype:32; /*!< precise type; MySQL data
type, charset code, flags to
indicate nullability,
signedness, whether this is a
binary string, whether this is
a true VARCHAR where MySQL
uses 2 bytes to store the length */
unsigned mtype:8; /*!< main data type */
/* the remaining fields do not affect alphabetical ordering: */
unsigned len:16; /*!< length; for MySQL data this
is field->pack_length(),
except that for a >= 5.0.3
type true VARCHAR this is the
maximum byte length of the
string data (in addition to
the string, MySQL uses 1 or 2
bytes to store the string length) */
unsigned mbminmaxlen:5; /*!< minimum and maximum length of a
character, in bytes;
DATA_MBMINMAXLEN(mbminlen,mbmaxlen);
mbminlen=DATA_MBMINLEN(mbminmaxlen);
mbmaxlen=DATA_MBMINLEN(mbminmaxlen) */
/*----------------------*/
/* End of definitions copied from dtype_t */
/* @} */
unsigned ind:10; /*!< table column position
(starting from 0) */
unsigned ord_part:1; /*!< nonzero if this column
appears in the ordering fields
of an index */
unsigned max_prefix:12; /*!< maximum index prefix length on
this column. Our current max limit is
3072 for Barracuda table */
};
dfield_t
/** Structure for an SQL data field */
struct dfield_t{
void* data; /*!< pointer to data */
unsigned ext:1; /*!< TRUE=externally stored, FALSE=local */
unsigned spatial_status:2;
/*!< spatial status of externally stored field
in undo log for purge */
unsigned len; /*!< data length; UNIV_SQL_NULL if SQL null */
dtype_t type; /*!< type of data */
/** Create a deep copy of this object
@param[in] heap the memory heap in which the clone will be
created.
@return the cloned object. */
dfield_t* clone(mem_heap_t* heap);
};
建表时创建字段
在创建表时,先依据字段数分配内存,再填充各字段。
dict_mem_table_create
mem_heap_create //分配内存
mem_heap_alloc(heap, table->n_cols * sizeof(dict_col_t))); //设置字段结构内存
mem_heap_alloc(heap, n_v_cols * sizeof(*table->v_cols))); //设置虚字段结构内存
dict_mem_table_add_col
dict_add_col_name
dict_table_get_nth_col //获取指向字段所在内存位置指针
dict_mem_fill_column_struct //根据指针填充字段信息
or
dict_mem_table_add_v_col
if(stored)
dict_mem_table_add_s_col
.....
....