SQL 语句中最显而易见的部分是语句的文本。客户端还可以定义适用于发送到服务器执行的下一个语句的查询属性:The most visible part of an SQL statement is the text of the statement. Clients can also define query attributes that apply to the next statement sent to the server for execution:
属性在发送语句之前定义。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:
一个网页应用生成页面,这些页面生成数据库查询,并且对于每个查询必须跟踪生成该查询的页面的 URL。A web application produces pages that generate database queries, and for each query must track the URL of the page that generated it.
一个应用程序为每个查询传递额外的处理信息,以供插件使用,例如审计插件或查询重写插件。An application passes extra processing information with each query, for use by a plugin such as an audit plugin or query rewrite plugin.
MySQL 支持这些功能,无需使用诸如在查询字符串中包含特殊格式注释之类的变通方法。本节的其余部分将描述如何使用查询属性支持,包括必须满足的先决条件。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 C API 的应用程序通过调用mysql_bind_param()函数来定义查询属性。请参见mysql_bind_param()。其他 MySQL 连接器也可能提供查询属性支持。请参阅各个连接器的文档。Applications that use the MySQL C API define query attributes by
calling the mysql_bind_param()
function. See mysql_bind_param(). Other MySQL
connectors may also provide query-attribute support. See the
documentation for individual connectors.
mysql 客户端具有一个 query_attributes 命令,可定义多达 32 对属性名和属性值。请参见 第 6.5.1.2 节,“mysql 客户端命令”。The mysql client has a
query_attributes command that enables
defining up to 32 pairs of attribute names and values. See
Section 6.5.1.2, “mysql Client Commands”.
查询属性名称是使用由 character_set_client 系统变量指示的字符集传输的。Query attribute names are transmitted using the character set
indicated by the
character_set_client system
variable.
要在已定义属性的 SQL 语句中访问查询属性,请按照 使用查询属性的前提条件 中的说明安装 query_attributes 组件。该组件实现了一个 mysql_query_attribute_string() 可加载函数,该函数接受属性名称参数并返回属性值的字符串,如果属性不存在,则返回 NULL。请参见 查询属性可加载函数。To access query attributes within SQL statements for which
attributes have been defined, install the
query_attributes component as described in
Prerequisites for Using 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. See
Query Attribute Loadable Functions.
以下示例使用mysql客户端的query_attributes命令来定义属性名/值对,并使用mysql_query_attribute_string()函数按名称访问属性值。The following examples use the mysql client
query_attributes command to define attribute
name/value pairs, and the
mysql_query_attribute_string()
function to access attribute values by name.
此示例定义了两个名为n1和n2的属性。第一个SELECT显示了如何检索这些属性,并演示了检索不存在的属性(n3)会返回NULL。第二个SELECT显示属性不会在语句之间持续存在。This example defines two attributes named n1
and n2. The first SELECT
shows how to retrieve those attributes, and also demonstrates
that retrieving a nonexistent attribute (n3)
returns NULL. The second
SELECT shows that attributes do not persist
across statements.
mysql>query_attributes n1 v1 n2 v2;mysql>SELECTmysql_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>SELECTmysql_query_attribute_string('n1') AS 'attr 1',mysql_query_attribute_string('n2') AS 'attr 2';+--------+--------+ | attr 1 | attr 2 | +--------+--------+ | NULL | NULL | +--------+--------+
如第二个SELECT语句所示,在给定语句之前定义的属性仅对该语句可用,并且在语句执行后会被清除。要在多个语句中使用属性值,请将其赋值给变量。下面的示例演示了如何执行此操作,并说明属性值可以通过变量在后续语句中使用,但不能通过调用mysql_query_attribute_string()来获取: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. 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_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.
使用
PREPARE创建的语句无法使用属性。Attributes are not available to statements prepared withPREPARE.mysql_query_attribute_string()函数不能用于 DDL 语句。Themysql_query_attribute_string()function cannot be used in DDL statements.属性不会被复制。调用
mysql_query_attribute_string()函数的语句不会在所有服务器上获得相同的值。Attributes are not replicated. Statements that invoke themysql_query_attribute_string()function will not get the same value on all servers.
要在已定义属性的 SQL 语句中访问查询属性,必须安装 query_attributes 组件。可使用以下语句进行安装:To access query attributes within SQL statements for which
attributes have been defined, the
query_attributes component must be installed.
Do so using this statement:
INSTALL COMPONENT "file://component_query_attributes";
组件安装是一次性操作,无需在每次服务器启动时执行。INSTALL
COMPONENT 会加载组件,并将其注册到 mysql.component 系统表中,以便在后续服务器启动时加载该组件。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.
query_attributes 组件访问查询属性以实现 mysql_query_attribute_string() 函数。请参见 第 7.5.4 节,“查询属性组件”。The query_attributes component accesses query
attributes to implement a
mysql_query_attribute_string()
function. See Section 7.5.4, “Query Attribute Components”.
要卸载 query_attributes 组件,请使用以下语句:To uninstall the query_attributes component,
use this statement:
UNINSTALL COMPONENT "file://component_query_attributes";
UNINSTALL COMPONENT 会卸载组件,并将其从 mysql.component 系统表中注销,以便在后续服务器启动时不加载该组件。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.
因为安装和卸载 query_attributes 组件会安装和卸载该组件实现的 mysql_query_attribute_string() 函数,因此无需使用 CREATE
FUNCTION 或 DROP
FUNCTION 来执行此操作。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.
mysql_query_attribute_string(name)应用程序可以定义适用于下一次发送到服务器的查询的属性。
mysql_query_attribute_string()函数根据属性名称返回属性值的字符串。此函数使查询能够访问并结合适用于它的属性值。Applications can define attributes that apply to the next query sent to the server. Themysql_query_attribute_string()function returns an attribute value as a string, given the attribute name. This function enables a query to access and incorporate values of the attributes that apply to it.mysql_query_attribute_string()是通过安装query_attributes组件来安装的。参见 第 11.6 节,“查询属性”,其中还讨论了查询属性的目的和使用。mysql_query_attribute_string()is installed by installing thequery_attributescomponent. See Section 11.6, “Query Attributes”, which also discusses the purpose and use of query attributes.参数:Arguments:
name:属性名称。name: The attribute name.
返回值:Return value:
返回属性值的字符串,如果属性不存在则返回
NULL。Returns the attribute value as a string for success, orNULLif the attribute does not exist.示例:Example:
以下示例使用 mysql 客户端的
query_attributes命令来定义可通过mysql_query_attribute_string()检索的查询属性。SELECT显示检索不存在的属性 (n3) 返回NULL。The following example uses the mysql clientquery_attributescommand to define query attributes that can be retrieved bymysql_query_attribute_string(). TheSELECTshows that retrieving a nonexistent attribute (n3) returnsNULL.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 | +--------+--------+--------+