The most visible part of an SQL statement is the text of the statement. As of MySQL 8.0.23, clients can also define query attributes that apply to the next statement sent to the server for execution:SQL语句中最明显的部分是语句的文本。从MySQL 8.0.23开始,客户端还可以定义应用于发送到服务器执行的下一条语句的查询属性:
Attributes are defined prior to sending the statement.属性是在发送语句之前定义的。
Attributes exist until statement execution ends, at which point the attribute set is cleared.属性一直存在,直到语句执行结束,此时属性集被清除。
While attributes exist, they can be accessed on the server side.虽然存在属性,但可以在服务器端访问这些属性。
Examples of the ways query attributes may be used:可以使用查询属性的方式示例:
A web application produces pages that generate database queries, and for each query must track the URL of the page that generated it.web应用程序生成生成数据库查询的页面,并且对于每个查询都必须跟踪生成该查询的页面的URL。
An application passes extra processing information with each query, for use by a plugin such as an audit plugin or query rewrite plugin.应用程序为每个查询传递额外的处理信息,供审计插件或查询重写插件等插件使用。
MySQL supports these capabilities without the use of workarounds such as specially formatted comments included in query strings. The remainder of this section describes how to use query attribute support, including the prerequisites that must be satisfied.MySQL支持这些功能,而不需要使用变通方法,例如查询字符串中包含的特殊格式的注释。本节的其余部分介绍如何使用查询属性支持,包括必须满足的先决条件。
Applications that use the MySQL C API define query attributes by calling the 使用MySQL C API的应用程序通过调用mysql_bind_param()
function. MySQL_bind_param()
函数来定义查询属性。See mysql_bind_param(). Other MySQL connectors may also provide query-attribute support. See the documentation for individual connectors.请参见mysql_bind_param()
。其他MySQL连接器也可以提供查询属性支持。有关各个连接器,请参阅文档。
The mysql client has a mysql客户端有一个query_attributes
command that enables defining up to 32 pairs of attribute names and values. See Section 4.5.1.2, “mysql Client Commands”.query_attributes
命令,可以定义多达32对属性名称和值。请参阅第4.5.1.2节,“mysql客户端命令”。
Query attribute names are transmitted using the character set indicated by the 查询属性名称使用character_set_client
system variable.character_set_client
系统变量所指示的字符集进行传输。
To access query attributes within SQL statements for which attributes have been defined, install the 要访问已定义属性的SQL语句中的查询属性,请按照使用查询属性的前提条件中的说明安query_attributes
component as described in Prerequisites for Using Query Attributes. 装query_attributes
组件。The component implements a 该组件实现了一个mysql_query_attribute_string()
loadable function that takes an attribute name argument and returns the attribute value as a string, or NULL
if the attribute does not exist. mysql_query_attribute_string()
可加载函数,该函数接受一个属性名称参数并以字符串形式返回属性值,如果该属性不存在,则返回NULL
。See Query Attribute Loadable Functions.请参见查询属性可加载函数。
The following examples use the mysql client 以下示例使用mysql客户端query_attributes
command to define attribute name/value pairs, and the mysql_query_attribute_string()
function to access attribute values by name.query_attributes
命令定义属性名称/值对,并使用mysql_query_aattribute_string()
函数按名称访问属性值。
This example defines two attributes named 此示例定义了两个名为n1
and n2
. n1
和n2
的属性。The first 第一个SELECT
shows how to retrieve those attributes, and also demonstrates that retrieving a nonexistent attribute (n3
) returns NULL
. SELECT
展示了如何检索这些属性,还展示了检索不存在的属性(n3
)会返回NULL
。The second 第二个SELECT
shows that attributes do not persist across statements.SELECT
显示属性不会在语句之间持久存在。
mysql>query_attributes n1 v1 n2 v2;
mysql>SELECT
mysql_query_attribute_string('n1') AS 'attr 1',
mysql_query_attribute_string('n2') AS 'attr 2',
mysql_query_attribute_string('n3') AS 'attr 3';
+--------+--------+--------+ | attr 1 | attr 2 | attr 3 | +--------+--------+--------+ | v1 | v2 | NULL | +--------+--------+--------+ mysql>SELECT
mysql_query_attribute_string('n1') AS 'attr 1',
mysql_query_attribute_string('n2') AS 'attr 2';
+--------+--------+ | attr 1 | attr 2 | +--------+--------+ | NULL | NULL | +--------+--------+
As shown by the second 如第二个SELECT
statement, attributes defined prior to a given statement are available only to that statement and are cleared after the statement executes. To use an attribute value across multiple statements, assign it to a variable. SELECT
语句所示,在给定语句之前定义的属性仅对该语句可用,并在语句执行后清除。若要在多个语句中使用一个属性值,请将其分配给一个变量。The following example shows how to do this, and illustrates that attribute values are available in subsequent statements by means of the variables, but not by calling 以下示例显示了如何做到这一点,并说明了属性值在后续语句中可以通过变量获得,但不能通过调用mysql_query_attribute_string()
:mysql_query_attribute_string()
获得:
mysql>query_attributes n1 v1 n2 v2;
mysql>SET
@attr1 = mysql_query_attribute_string('n1'),
@attr2 = mysql_query_attribute_string('n2');
mysql>SELECT
@attr1, mysql_query_attribute_string('n1') AS 'attr 1',
@attr2, mysql_query_attribute_string('n2') AS 'attr 2';
+--------+--------+--------+--------+ | @attr1 | attr 1 | @attr2 | attr 2 | +--------+--------+--------+--------+ | v1 | NULL | v2 | NULL | +--------+--------+--------+--------+
Attributes can also be saved for later use by storing them in a table:还可以通过将属性存储在表中来保存属性以备日后使用:
mysql>CREATE TABLE t1 (c1 CHAR(20), c2 CHAR(20));
mysql>query_attributes n1 v1 n2 v2;
mysql>INSERT INTO t1 (c1, c2) VALUES(
mysql_query_attribute_string('n1'),
mysql_query_attribute_string('n2')
);
mysql>SELECT * FROM t1;
+------+------+ | c1 | c2 | +------+------+ | v1 | v2 | +------+------+
Query attributes are subject to these limitations and restrictions:查询属性受以下限制:
If multiple attribute-definition operations occur prior to sending a statement to the server for execution, the most recent definition operation applies and replaces attributes defined in earlier operations.如果在将语句发送到服务器执行之前发生了多个属性定义操作,则应用最新的定义操作并替换先前操作中定义的属性。
If multiple attributes are defined with the same name, attempts to retrieve the attribute value have an undefined result.如果使用相同的名称定义了多个属性,则尝试检索属性值会产生未定义的结果。
An attribute defined with an empty name cannot be retrieved by name.使用空名称定义的属性无法按名称检索。
Attributes are not available to statements prepared with 属性不可用于使用PREPARE
.PREPARE
准备的语句。
The 在DDL语句中不能使用mysql_query_attribute_string()
function cannot be used in DDL statements.mysql_query_attribute_string()
函数。
Attributes are not replicated. Statements that invoke the 不复制属性。调用mysql_query_attribute_string()
function will not get the same value on all servers.mysql_query_attribute_string()
函数的语句不会在所有服务器上获得相同的值。
To access query attributes within SQL statements for which attributes have been defined, the 若要访问已定义属性的SQL语句中的查询属性,必须安装query_attributes
component must be installed. Do so using this statement:query_attributes
组件。使用以下语句执行此操作:
INSTALL COMPONENT "file://component_query_attributes";
Component installation is a one-time operation that need not be done per server startup. 组件安装是一次性操作,不需要每次启动服务器就完成。INSTALL COMPONENT
loads the component, and also registers it in the mysql.component
system table to cause it to be loaded during subsequent server startups.INSTALL COMPONENT
加载组件,并将其注册在mysql.component
系统表中,以便在随后的服务器启动过程中加载。
The query_attributes
component accesses query attributes to implement a mysql_query_attribute_string()
function. query_attributes
组件访问查询属性以实现一个mysql_query_aattribute_string()
函数。See Section 5.5.4, “Query Attribute Components”.请参见第5.5.4节,“查询属性组件”。
To uninstall the 要卸载query_attributes
component, use this statement:query_attributes
组件,请使用以下语句:
UNINSTALL COMPONENT "file://component_query_attributes";
UNINSTALL COMPONENT
unloads the component, and unregisters it from the mysql.component
system table to cause it not to be loaded during subsequent server startups.UNINSTALL COMPONENT
将卸载该组件,并从mysql.component
系统表中注销该组件,以便在随后的服务器启动过程中不加载该组件。
Because installing and uninstalling the 因为安装和卸载query_attributes
component installs and uninstalls the mysql_query_attribute_string()
function that the component implements, it is not necessary to use CREATE FUNCTION
or DROP FUNCTION
to do so.query_attributes
组件会安装和卸载该组件实现的mysql_query_aattribute_string()
函数,所以不需要使用CREATE FUNCTION
或DROP FUNCTION
。
mysql_query_attribute_string(
name
)
Applications can define attributes that apply to the next query sent to the server. 应用程序可以定义应用于发送到服务器的下一个查询的属性。The mysql_query_attribute_string()
function, available as of MySQL 8.0.23, returns an attribute value as a string, given the attribute name. mysql_query_attribute_string()
函数从mysql 8.0.23开始提供,在给定属性名称的情况下,以字符串形式返回属性值。This function enables a query to access and incorporate values of the attributes that apply to it.此函数使查询能够访问并合并应用于它的属性值。
mysql_query_attribute_string()
is installed by installing the query_attributes
component. mysql_query_attribute_string()
是通过安装query_aattributes
组件来安装的。See Section 9.6, “Query Attributes”, which also discusses the purpose and use of query attributes.请参阅第9.6节,“查询属性”,其中还讨论了查询属性的目的和使用。
Arguments:参数:
name
: The attribute name.:属性名称。
Return value:返回值:
Returns the attribute value as a string for success, or 如果成功,则以字符串形式返回属性值;如果属性不存在,则返回NULL
if the attribute does not exist.NULL
。
Example:示例:
The following example uses the mysql client 以下示例使用mysql客户端query_attributes
command to define query attributes that can be retrieved by mysql_query_attribute_string()
. query_attributes
命令来定义可以由mysql_query_aattribute_string()
检索的查询属性。The SELECT
shows that retrieving a nonexistent attribute (n3
) returns NULL
.SELECT
显示检索不存在的属性(n3
)返回NULL
。
mysql> query_attributes n1 v1 n2 v2; mysql> SELECT -> mysql_query_attribute_string('n1') AS 'attr 1', -> mysql_query_attribute_string('n2') AS 'attr 2', -> mysql_query_attribute_string('n3') AS 'attr 3'; +--------+--------+--------+ | attr 1 | attr 2 | attr 3 | +--------+--------+--------+ | v1 | v2 | NULL | +--------+--------+--------+