MySQL 9.7.0
Source Code Documentation
sql_masking_policy.h File Reference
#include <optional>
#include <string>
#include "lex_string.h"
#include "my_inttypes.h"

Go to the source code of this file.

Classes

struct  Sql_masking_policy_spec
 

Functions

std::optional< Sql_masking_policy_specget_masking_policy_spec (THD *thd, LEX_CSTRING policy_name, std::string *reason)
 Returns the masking policy with the given name if it can be found. More...
 
bool check_masking_policy_manage_privilege (THD *thd)
 Checks if the current user has the MANAGE_DATA_MASKING_POLICY privilege. More...
 
bool drop_masking_policy (THD *thd, LEX_CSTRING policy_name, bool if_exists)
 Drop the masking policy with the given name. More...
 
bool create_masking_policy (THD *thd, bool if_not_exists, const Sql_masking_policy_spec &spec)
 Create a masking policy with the given specification. More...
 
bool check_masking_policy_name (LEX_CSTRING name)
 Check if the name is valid for a masking policy name or a masking policy argument name. More...
 
bool validate_masking_policy_syntax (THD *thd, LEX_CSTRING argument_name, Item *expr)
 Validate structural and semantic restrictions for a masking policy expression. More...
 
Itemresolve_masking_expression (THD *thd, Item_field *item_field, const Sql_masking_policy_spec &spec)
 Parse and resolve the column’s masking expression under the column’s security context. More...
 
bool validate_masking_policy_for_create_alter_table (THD *thd, uchar *buf, TABLE *table, const Create_field &field)
 Validates masking policies for CREATE/ALTER TABLE. More...
 

Function Documentation

◆ check_masking_policy_manage_privilege()

bool check_masking_policy_manage_privilege ( THD thd)

Checks if the current user has the MANAGE_DATA_MASKING_POLICY privilege.

Raises an error if the privilege is missing.

Parameters
thdThread context
Return values
falsePrivilege is granted
truePrivilege is missing (error is reported)

◆ check_masking_policy_name()

bool check_masking_policy_name ( LEX_CSTRING  name)

Check if the name is valid for a masking policy name or a masking policy argument name.

Raises an error if it is not valid.

Parameters
nameName to validate
Return values
falseName is valid
trueValidation failed (error is reported)

◆ create_masking_policy()

bool create_masking_policy ( THD thd,
bool  if_not_exists,
const Sql_masking_policy_spec spec 
)

Create a masking policy with the given specification.

Parameters
thdThread context
if_not_existsTrue if CREATE MASKING POLICY IF NOT EXIST, which means it is not an error if the policy already exists. The existing policy is kept.
specMasking policy specification
Return values
falseOn success
trueIf an error has been raised

◆ drop_masking_policy()

bool drop_masking_policy ( THD thd,
LEX_CSTRING  policy_name,
bool  if_exists 
)

Drop the masking policy with the given name.

Parameters
thdThread context
policy_nameThe name of the policy to drop
if_existsTrue if DROP IF EXISTS, which means it is not an error to call it with a policy name that does not exist.
Return values
trueIf an error is raised
falseOn success

◆ get_masking_policy_spec()

std::optional< Sql_masking_policy_spec > get_masking_policy_spec ( THD thd,
LEX_CSTRING  policy_name,
std::string *  reason 
)

Returns the masking policy with the given name if it can be found.

Returns an empty result if the masking policy with that name could not be found. No error is raised, but the output parameter reason contains the reason why the masking policy was not found.

Parameters
thdThread context
policy_nameThe name of the policy to look for
[out]reasonIf the function returns an empty value, this parameter will return the reason why the policy was not found.
Returns
The policy specification if found; otherwise empty.

◆ resolve_masking_expression()

Item * resolve_masking_expression ( THD thd,
Item_field item_field,
const Sql_masking_policy_spec spec 
)

Parse and resolve the column’s masking expression under the column’s security context.

Replaces the policy argument with the actual Item_field and keeps name resolution otherwise empty to prevent references to other columns.

Parameters
thdThread context
item_fieldColumn reference substituted for the policy argument
specMasking policy specification (previously fetched)
Returns
a pointer to the resolved Item on success, or nullptr on error (error is reported)

◆ validate_masking_policy_for_create_alter_table()

bool validate_masking_policy_for_create_alter_table ( THD thd,
uchar buf,
TABLE table,
const Create_field field 
)

Validates masking policies for CREATE/ALTER TABLE.

Performs validation in three categories and delegates details to helpers:

  • Column eligibility for masking (validate_masking_policy_column_constraints()).
  • Masking function resolution and post-resolve validation (validate_masking_function_post_resolve()).
  • Column/function type compatibility (compatible_types()).

See the referenced helpers for detailed rules and rationale.

Parameters
thdThread context
bufRow buffer used to back a temporary Field instance
tableTable being created/altered
fieldColumn definition being validated
Return values
trueValidation failed (error was reported)
falseValidation succeeded

◆ validate_masking_policy_syntax()

bool validate_masking_policy_syntax ( THD thd,
LEX_CSTRING  argument_name,
Item expr 
)

Validate structural and semantic restrictions for a masking policy expression.

Rules enforced:

  • Must use the form CASE WHEN <CURRENT_USER_IN|CURRENT_ROLE_IN>(...) THEN <expr> ELSE <expr>
  • Exactly one WHEN clause and a required ELSE clause
  • THEN/ELSE must meet generated-column-like rules (UDFs allowed)
  • Only the policy argument may reference a column
  • Either THEN or ELSE must return the unmasked value
Parameters
thdThread context
argument_nameName of the argument used in the policy
exprExpression tree of the policy
Return values
trueValidation failed (error reported)
falseValidation succeeded