MySQL Internals Manual Chapter 10 : 10.1 The Item Class

本文深入解析MySQL中的Item类,介绍了其在MySQL源码中的定义及作用,并详细列举了Item类的各种子类及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值