在MySQL中,Item 是一个 类,在MySQL源码中具有明确的定义。 Item类的每个实例都有
- SQL( an analogue in the SQL language)
- 值 (value)
- 数据类型(a data type descriptor)
以下这些都可以用Item类表示
- 文字(literals)
- 列引用(column references)
- 会话或全局变量(session or global variables)
- 过程变量(procedure variables)
- 参数(parameters)
- SQL函数(SQL functions,包括+,||,=,LIKE等运算符)
例如:以下SQL
SELECT UPPER(column1) FROM t WHERE column2 = @x;
MySQL需要存储SELECT的Item列表(‘column1’ column reference and UPPER function)
以及WHERE子句的Item列表(‘column2’ column reference and ‘@x’ variable and ‘=’ operator)
其中有两个术语:
site:与item的实例大致对应,表示保存指定数据类型的值的实例的位置
field:表示column reference
MySQL’s Item class 定义在 …/sql/item.h
它的子类定义在 …/sql/item*.h
Item_ident (Item_field, Item_ref)
Item_null
Item_num (Item_int, Item_real)
Item_param
Item_string (Item_static_string_func, Item_datetime, Item_empty_string)
Item_hex_string (Item_bin_string)
Item_result_field (all "item_func.h" "item_subselect.h" "item_sub.h" classes)
Item_copy_string
Item_cache (Item_cache_int, Item_cache_real, Item_cache_str, Item_cache_row)
Item_type_holder
Item_row
没有正式标准对这些子类进行分类,主要还是按用途(字段,参数,函数)和数据类型(数字,字符串)进行分类。
那么,MySQL如何使用Item的呢?我们发现/sql目录中几乎每个.cc程序都使用了Item类及其子类。以下列出部分使用情况。
sql_parse.cc: Makes new items in add_field_to_list()
item_sum.cc: Uses item_func subclasses for COUNT, AVG, SUM
item_buff.cc: Where buffers for item values can be stored
item_cmpfunc.cc: Comparison functions with item_func subclasses
item_create.cch: For creating items that the lex might use
item_subselect.cc: Subqueries are another type of function
mysqld.cc: When main() ends, it uses clean_up() for items
opt_range.cc: Uses field, compare-condition, and value subclasses
procedure.cc: Notice Procedure * has a pointer to an item list
protocol.cc: Uses send_fields() to pass item values back to users
sys_var.cc: System variables have Item associations too
sql_base.cc: Thread-specific Item searchers like find_field_in_table()
sql_class.cc: Look at cleanup_after_query()
sql_delete.cc: This (like sql_insert.cc etc.) has field references
sql_error.cc: Has one of many examples of SHOW's use of items
sql_lex.cc: Notice "add...to_list" functions
sql_select.cc: The largest program that uses items, apparently
udf_example.cc: The comments in this program are extensive
每当需要分配,比较,汇总,接收,发送或验证site的SQL操作时,我们都会发现MySQL对Item及其子类的使用。
原文:https://dev.mysql.com/doc/internals/en/item-class.html