MySQL 8.0.40
Source Code Documentation
sql_view.cc File Reference
#include "sql/sql_view.h"
#include <sys/types.h>
#include <climits>
#include <cstdio>
#include <cstring>
#include <utility>
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/mysql_lex_string.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/binlog.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/dd.h"
#include "sql/dd/dd_schema.h"
#include "sql/dd/dd_view.h"
#include "sql/dd/dictionary.h"
#include "sql/dd/string_type.h"
#include "sql/dd/types/abstract_table.h"
#include "sql/dd/types/view.h"
#include "sql/dd_sql_view.h"
#include "sql/derror.h"
#include "sql/enum_query_type.h"
#include "sql/error_handler.h"
#include "sql/field.h"
#include "sql/item.h"
#include "sql/key.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/opt_trace.h"
#include "sql/parse_tree_node_base.h"
#include "sql/parser_yystype.h"
#include "sql/query_options.h"
#include "sql/sp_cache.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h"
#include "sql/sql_show.h"
#include "sql/sql_table.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h"
#include "sql_string.h"
#include "thr_lock.h"

Classes

class  Make_view_tracker
 RAII class to ease error handling in parse_view_definition() More...
 
class  DD_table_access_error_handler
 This internal handler is used to trap ER_NO_SYSTEM_TABLE_ACCESS. More...
 

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 

Functions

static void make_unique_view_field_name (Item *target, const mem_root_deque< Item * > &item_list, Item *last_element)
 
bool check_duplicate_names (const Create_col_name_list *column_names, const mem_root_deque< Item * > &item_list, bool gen_unique_view_name)
 When creating a derived table, check if duplicate column names are present, and possibly generate unique names instead. More...
 
void make_valid_column_names (LEX *lex)
 Check if auto generated column names are conforming and possibly generate a conforming name for them if not. More...
 
static bool fill_defined_view_parts (THD *thd, Table_ref *view)
 
bool create_view_precheck (THD *thd, Table_ref *tables, Table_ref *view, enum_view_create_mode mode)
 CREATE VIEW privileges pre-check. More...
 
bool mysql_create_view (THD *thd, Table_ref *views, enum_view_create_mode mode)
 Creating/altering VIEW procedure. More...
 
bool is_updatable_view (THD *thd, Table_ref *view)
 
bool mysql_register_view (THD *thd, Table_ref *view, enum_view_create_mode mode)
 Register view by writing its definition to the data-dictionary. More...
 
bool open_and_read_view (THD *thd, TABLE_SHARE *share, Table_ref *view_ref)
 Open and read a view definition. More...
 
void merge_query_blocks (LEX *view_lex, LEX *parent_lex)
 Merge a view query expression into the parent expression. More...
 
bool parse_view_definition (THD *thd, Table_ref *view_ref)
 Parse a view definition. More...
 
bool mysql_drop_view (THD *thd, Table_ref *views)
 Drop view. More...
 
bool check_key_in_view (THD *thd, Table_ref *view, const Table_ref *table_ref)
 check of key (primary or unique) presence in updatable view More...
 
bool insert_view_fields (mem_root_deque< Item * > *list, Table_ref *view)
 

Function Documentation

◆ check_duplicate_names()

bool check_duplicate_names ( const Create_col_name_list column_names,
const mem_root_deque< Item * > &  item_list,
bool  gen_unique_view_name 
)

When creating a derived table, check if duplicate column names are present, and possibly generate unique names instead.

Parameters
column_namesUser-provided list of column names, NULL if none
item_listSELECT list of underlying query expression
gen_unique_view_nameSee description.
  • If a list of column names has been provided: it is simply searched for duplicates (which cause an error).
  • otherwise, column names are derived from the underlying query expression's SELECT list elements; if two of those elements have duplicate autogenerated names: if gen_unique_view_name is false: error it it is true: we generate a unique name using make_unique_view_field_name()
Returns
true if error.

◆ check_key_in_view()

bool check_key_in_view ( THD thd,
Table_ref view,
const Table_ref table_ref 
)

check of key (primary or unique) presence in updatable view

If the table to be checked is a view and the query has LIMIT clause, then check that the view fulfills one of the following constraints: 1) it contains the primary key of the underlying updatable table. 2) it contains a unique key of the underlying updatable table whose columns are all non-nullable. 3) it contains all columns of the underlying updatable table.

Parameters
thdthread handler
viewview for check with opened table
table_refunderlying updatable table of the view
Returns
false is success, true if error

◆ create_view_precheck()

bool create_view_precheck ( THD thd,
Table_ref tables,
Table_ref view,
enum_view_create_mode  mode 
)

CREATE VIEW privileges pre-check.

Parameters
thdthread handler
tablestables used in the view
viewviews to create
modeVIEW_CREATE_NEW, VIEW_ALTER, VIEW_CREATE_OR_REPLACE
Return values
falseOperation was a success.
trueAn error occurred.

◆ fill_defined_view_parts()

static bool fill_defined_view_parts ( THD thd,
Table_ref view 
)
static

◆ insert_view_fields()

bool insert_view_fields ( mem_root_deque< Item * > *  list,
Table_ref view 
)

◆ is_updatable_view()

bool is_updatable_view ( THD thd,
Table_ref view 
)
See also
Query_block::merge_derived()

◆ make_unique_view_field_name()

static void make_unique_view_field_name ( Item target,
const mem_root_deque< Item * > &  item_list,
Item last_element 
)
static

◆ make_valid_column_names()

void make_valid_column_names ( LEX lex)

Check if auto generated column names are conforming and possibly generate a conforming name for them if not.

Parameters
lexLEX for this thread.

◆ merge_query_blocks()

void merge_query_blocks ( LEX view_lex,
LEX parent_lex 
)

Merge a view query expression into the parent expression.

Update all LEX pointers inside the view expression to point to the parent LEX.

Parameters
view_lexView's LEX object.
parent_lexOriginal LEX object.

◆ mysql_create_view()

bool mysql_create_view ( THD thd,
Table_ref views,
enum_view_create_mode  mode 
)

Creating/altering VIEW procedure.

Atomicity: The operation to create, alter and create_or_replace a view is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. Data-dictionary and table definition caches are in sync with operation state. Cache do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdthread handler
viewsviews to create
modeVIEW_CREATE_NEW, VIEW_ALTER, VIEW_CREATE_OR_REPLACE
Note
This function handles both create and alter view commands.
Return values
falseOperation was a success.
trueAn error occurred.

◆ mysql_drop_view()

bool mysql_drop_view ( THD thd,
Table_ref views 
)

Drop view.

Atomicity: The operation to drop a view is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. Data-dictionary and table definition caches are in sync with operation state. Cache do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log. The partial execution of a drop view statement is not supported any more with atomic drop view implementation.

Parameters
[in]thdthread handler
[in]viewsviews to delete
Return values
falseOK
trueError

◆ mysql_register_view()

bool mysql_register_view ( THD thd,
Table_ref view,
enum_view_create_mode  mode 
)

Register view by writing its definition to the data-dictionary.

Parameters
thdThread handler.
viewView description
modeVIEW_CREATE_NEW, VIEW_ALTER or VIEW_CREATE_OR_REPLACE.
Note
The caller must rollback both statement and transaction on failure, before any further accesses to DD. This is because such a failure might be caused by a deadlock, which requires rollback before any other operations on SE (including reads using attachable transactions) can be done.
Return values
falseOK
trueError
See also
Item::print_for_order()

◆ open_and_read_view()

bool open_and_read_view ( THD thd,
TABLE_SHARE share,
Table_ref view_ref 
)

Open and read a view definition.

Parameters
[in]thdThread handler
[in]shareShare object of view
[in,out]view_refTable_ref structure for view reference
Returns
false-in case of success, true-in case of error.
Note
In case true value returned an error has been already set in DA.

◆ parse_view_definition()

bool parse_view_definition ( THD thd,
Table_ref view_ref 
)

Parse a view definition.

Among other effects, it adds underlying tables to the global list of tables, so the next iteration in open_tables() will open them.

Parameters
[in]thdThread handler
[in,out]view_refTable_ref structure for view reference
Returns
false-in case of success, true-in case of error.
Note
In case true value returned an error has been already set in DA.