1/*****************************************************************************
2
3Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, 2018, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/**************************************************//**
21@file include/dict0crea.h
22Database object creation
23
24Created 1/8/1996 Heikki Tuuri
25*******************************************************/
26
27#ifndef dict0crea_h
28#define dict0crea_h
29
30#include "univ.i"
31#include "dict0types.h"
32#include "dict0dict.h"
33#include "que0types.h"
34#include "row0types.h"
35#include "mtr0mtr.h"
36#include "fil0crypt.h"
37#include "fsp0space.h"
38
39/*********************************************************************//**
40Creates a table create graph.
41@return own: table create node */
42tab_node_t*
43tab_create_graph_create(
44/*====================*/
45 dict_table_t* table, /*!< in: table to create, built as
46 a memory data structure */
47 mem_heap_t* heap, /*!< in: heap where created */
48 fil_encryption_t mode, /*!< in: encryption mode */
49 uint32_t key_id); /*!< in: encryption key_id */
50
51/** Creates an index create graph.
52@param[in] index index to create, built as a memory data structure
53@param[in] table table name
54@param[in,out] heap heap where created
55@param[in] add_v new virtual columns added in the same clause with
56 add index
57@return own: index create node */
58ind_node_t*
59ind_create_graph_create(
60 dict_index_t* index,
61 const char* table,
62 mem_heap_t* heap,
63 const dict_add_v_col_t* add_v = NULL);
64
65/***********************************************************//**
66Creates a table. This is a high-level function used in SQL execution graphs.
67@return query thread to run next or NULL */
68que_thr_t*
69dict_create_table_step(
70/*===================*/
71 que_thr_t* thr); /*!< in: query thread */
72
73/** Assign a new table ID and put it into the table cache and the transaction.
74@param[in,out] table Table that needs an ID
75@param[in,out] trx Transaction */
76void
77dict_table_assign_new_id(
78 dict_table_t* table,
79 trx_t* trx);
80
81/***********************************************************//**
82Creates an index. This is a high-level function used in SQL execution
83graphs.
84@return query thread to run next or NULL */
85que_thr_t*
86dict_create_index_step(
87/*===================*/
88 que_thr_t* thr); /*!< in: query thread */
89
90/***************************************************************//**
91Builds an index definition but doesn't update sys_table.
92@return DB_SUCCESS or error code */
93void
94dict_build_index_def(
95/*=================*/
96 const dict_table_t* table, /*!< in: table */
97 dict_index_t* index, /*!< in/out: index */
98 trx_t* trx); /*!< in/out: InnoDB transaction
99 handle */
100/***************************************************************//**
101Creates an index tree for the index if it is not a member of a cluster.
102Don't update SYSTEM TABLES.
103@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
104dberr_t
105dict_create_index_tree(
106/*===================*/
107 dict_index_t* index, /*!< in/out: index */
108 const trx_t* trx); /*!< in: InnoDB transaction handle */
109
110/*******************************************************************//**
111Recreate the index tree associated with a row in SYS_INDEXES table.
112@return new root page number, or FIL_NULL on failure */
113ulint
114dict_recreate_index_tree(
115/*======================*/
116 const dict_table_t* table, /*!< in: the table the index
117 belongs to */
118 btr_pcur_t* pcur, /*!< in/out: persistent cursor pointing
119 to record in the clustered index of
120 SYS_INDEXES table. The cursor may be
121 repositioned in this call. */
122 mtr_t* mtr); /*!< in: mtr having the latch
123 on the record page. The mtr may be
124 committed and restarted in this call. */
125
126/** Drop the index tree associated with a row in SYS_INDEXES table.
127@param[in,out] rec SYS_INDEXES record
128@param[in,out] pcur persistent cursor on rec
129@param[in,out] mtr mini-transaction
130@return whether freeing the B-tree was attempted */
131bool
132dict_drop_index_tree(
133 rec_t* rec,
134 btr_pcur_t* pcur,
135 mtr_t* mtr);
136
137/***************************************************************//**
138Creates an index tree for the index if it is not a member of a cluster.
139Don't update SYSTEM TABLES.
140@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
141dberr_t
142dict_create_index_tree_in_mem(
143/*==========================*/
144 dict_index_t* index, /*!< in/out: index */
145 const trx_t* trx); /*!< in: InnoDB transaction handle */
146
147/****************************************************************//**
148Creates the foreign key constraints system tables inside InnoDB
149at server bootstrap or server start if they are not found or are
150not of the right form.
151@return DB_SUCCESS or error code */
152dberr_t
153dict_create_or_check_foreign_constraint_tables(void);
154/*================================================*/
155
156/********************************************************************//**
157Generate a foreign key constraint name when it was not named by the user.
158A generated constraint has a name of the format dbname/tablename_ibfk_NUMBER,
159where the numbers start from 1, and are given locally for this table, that is,
160the number is not global, as it used to be before MySQL 4.0.18. */
161UNIV_INLINE
162dberr_t
163dict_create_add_foreign_id(
164/*=======================*/
165 ulint* id_nr, /*!< in/out: number to use in id
166 generation; incremented if used */
167 const char* name, /*!< in: table name */
168 dict_foreign_t* foreign); /*!< in/out: foreign key */
169
170/** Adds the given set of foreign key objects to the dictionary tables
171in the database. This function does not modify the dictionary cache. The
172caller must ensure that all foreign key objects contain a valid constraint
173name in foreign->id.
174@param[in] local_fk_set set of foreign key objects, to be added to
175the dictionary tables
176@param[in] table table to which the foreign key objects in
177local_fk_set belong to
178@param[in,out] trx transaction
179@return error code or DB_SUCCESS */
180dberr_t
181dict_create_add_foreigns_to_dictionary(
182/*===================================*/
183 const dict_foreign_set& local_fk_set,
184 const dict_table_t* table,
185 trx_t* trx)
186 MY_ATTRIBUTE((nonnull, warn_unused_result));
187
188/** Check if a foreign constraint is on columns server as base columns
189of any stored column. This is to prevent creating SET NULL or CASCADE
190constraint on such columns
191@param[in] local_fk_set set of foreign key objects, to be added to
192the dictionary tables
193@param[in] table table to which the foreign key objects in
194local_fk_set belong to
195@return true if yes, otherwise, false */
196bool
197dict_foreigns_has_s_base_col(
198 const dict_foreign_set& local_fk_set,
199 const dict_table_t* table);
200
201/****************************************************************//**
202Creates the tablespaces and datafiles system tables inside InnoDB
203at server bootstrap or server start if they are not found or are
204not of the right form.
205@return DB_SUCCESS or error code */
206dberr_t
207dict_create_or_check_sys_tablespace(void);
208/*=====================================*/
209/** Creates the virtual column system tables inside InnoDB
210at server bootstrap or server start if they are not found or are
211not of the right form.
212@return DB_SUCCESS or error code */
213dberr_t
214dict_create_or_check_sys_virtual();
215
216/** Put a tablespace definition into the data dictionary,
217replacing what was there previously.
218@param[in] space Tablespace id
219@param[in] name Tablespace name
220@param[in] flags Tablespace flags
221@param[in] path Tablespace path
222@param[in] trx Transaction
223@return error code or DB_SUCCESS */
224dberr_t
225dict_replace_tablespace_in_dictionary(
226 ulint space_id,
227 const char* name,
228 ulint flags,
229 const char* path,
230 trx_t* trx);
231
232/** Delete records from SYS_TABLESPACES and SYS_DATAFILES associated
233with a particular tablespace ID.
234@param[in] space Tablespace ID
235@param[in,out] trx Current transaction
236@return DB_SUCCESS if OK, dberr_t if the operation failed */
237dberr_t
238dict_delete_tablespace_and_datafiles(
239 ulint space,
240 trx_t* trx);
241
242/********************************************************************//**
243Add a foreign key definition to the data dictionary tables.
244@return error code or DB_SUCCESS */
245dberr_t
246dict_create_add_foreign_to_dictionary(
247/*==================================*/
248 const char* name, /*!< in: table name */
249 const dict_foreign_t* foreign,/*!< in: foreign key */
250 trx_t* trx) /*!< in/out: dictionary transaction */
251 MY_ATTRIBUTE((nonnull, warn_unused_result));
252
253/********************************************************************//**
254Construct foreign key constraint defintion from data dictionary information.
255*/
256UNIV_INTERN
257char*
258dict_foreign_def_get(
259/*=================*/
260 dict_foreign_t* foreign,/*!< in: foreign */
261 trx_t* trx); /*!< in: trx */
262
263/* Table create node structure */
264struct tab_node_t{
265 que_common_t common; /*!< node type: QUE_NODE_TABLE_CREATE */
266 dict_table_t* table; /*!< table to create, built as a
267 memory data structure with
268 dict_mem_... functions */
269 ins_node_t* tab_def; /*!< child node which does the insert of
270 the table definition; the row to be
271 inserted is built by the parent node */
272 ins_node_t* col_def; /*!< child node which does the inserts
273 of the column definitions; the row to
274 be inserted is built by the parent
275 node */
276 ins_node_t* v_col_def; /*!< child node which does the inserts
277 of the sys_virtual row definitions;
278 the row to be inserted is built by
279 the parent node */
280 /*----------------------*/
281 /* Local storage for this graph node */
282 ulint state; /*!< node execution state */
283 ulint col_no; /*!< next column definition to insert */
284 uint key_id; /*!< encryption key_id */
285 fil_encryption_t mode; /*!< encryption mode */
286 ulint base_col_no; /*!< next base column to insert */
287 mem_heap_t* heap; /*!< memory heap used as auxiliary
288 storage */
289};
290
291/* Table create node states */
292#define TABLE_BUILD_TABLE_DEF 1
293#define TABLE_BUILD_COL_DEF 2
294#define TABLE_BUILD_V_COL_DEF 3
295#define TABLE_ADD_TO_CACHE 4
296#define TABLE_COMPLETED 5
297
298/* Index create node struct */
299
300struct ind_node_t{
301 que_common_t common; /*!< node type: QUE_NODE_INDEX_CREATE */
302 dict_index_t* index; /*!< index to create, built as a
303 memory data structure with
304 dict_mem_... functions */
305 const char* table_name; /*!< table name */
306 ins_node_t* ind_def; /*!< child node which does the insert of
307 the index definition; the row to be
308 inserted is built by the parent node */
309 ins_node_t* field_def; /*!< child node which does the inserts
310 of the field definitions; the row to
311 be inserted is built by the parent
312 node */
313 /*----------------------*/
314 /* Local storage for this graph node */
315 ulint state; /*!< node execution state */
316 ulint page_no; /* root page number of the index */
317 dict_table_t* table; /*!< table which owns the index */
318 dtuple_t* ind_row; /* index definition row built */
319 ulint field_no; /* next field definition to insert */
320 mem_heap_t* heap; /*!< memory heap used as auxiliary
321 storage */
322 const dict_add_v_col_t*
323 add_v; /*!< new virtual columns that being
324 added along with an add index call */
325};
326
327/** Compose a column number for a virtual column, stored in the "POS" field
328of Sys_columns. The column number includes both its virtual column sequence
329(the "nth" virtual column) and its actual column position in original table
330@param[in] v_pos virtual column sequence
331@param[in] col_pos column position in original table definition
332@return composed column position number */
333UNIV_INLINE
334ulint
335dict_create_v_col_pos(
336 ulint v_pos,
337 ulint col_pos);
338
339/** Get the column number for a virtual column (the column position in
340original table), stored in the "POS" field of Sys_columns
341@param[in] pos virtual column position
342@return column position in original table */
343UNIV_INLINE
344ulint
345dict_get_v_col_mysql_pos(
346 ulint pos);
347
348/** Get a virtual column sequence (the "nth" virtual column) for a
349virtual column, stord in the "POS" field of Sys_columns
350@param[in] pos virtual column position
351@return virtual column sequence */
352UNIV_INLINE
353ulint
354dict_get_v_col_pos(
355 ulint pos);
356
357/* Index create node states */
358#define INDEX_BUILD_INDEX_DEF 1
359#define INDEX_BUILD_FIELD_DEF 2
360#define INDEX_CREATE_INDEX_TREE 3
361#define INDEX_ADD_TO_CACHE 4
362
363#include "dict0crea.ic"
364
365#endif
366