1 /* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "sql/dd/impl/tables/view_routine_usage.h"
24 
25 #include <new>
26 #include <string>
27 
28 #include "sql/dd/impl/raw/object_keys.h"       // dd::Parent_id_range_key
29 #include "sql/dd/impl/tables/dd_properties.h"  // TARGET_DD_VERSION
30 #include "sql/dd/impl/types/object_table_definition_impl.h"
31 #include "sql/mysqld.h"
32 #include "sql/stateless_allocator.h"
33 
34 namespace dd {
35 namespace tables {
36 
37 ///////////////////////////////////////////////////////////////////////////
38 
instance()39 const View_routine_usage &View_routine_usage::instance() {
40   static View_routine_usage *s_instance =
41       new (std::nothrow) View_routine_usage();
42   return *s_instance;
43 }
44 
45 ///////////////////////////////////////////////////////////////////////////
46 
View_routine_usage()47 View_routine_usage::View_routine_usage() {
48   m_target_def.set_table_name("view_routine_usage");
49 
50   m_target_def.add_field(FIELD_VIEW_ID, "FIELD_VIEW_ID",
51                          "view_id BIGINT UNSIGNED NOT NULL");
52   m_target_def.add_field(
53       FIELD_ROUTINE_CATALOG, "FIELD_ROUTINE_CATALOG",
54       "routine_catalog VARCHAR(64) NOT NULL COLLATE " +
55           String_type(Object_table_definition_impl::fs_name_collation()->name));
56   m_target_def.add_field(
57       FIELD_ROUTINE_SCHEMA, "FIELD_ROUTINE_SCHEMA",
58       "routine_schema VARCHAR(64) NOT NULL COLLATE " +
59           String_type(Object_table_definition_impl::fs_name_collation()->name));
60   m_target_def.add_field(FIELD_ROUTINE_NAME, "FIELD_ROUTINE_NAME",
61                          "routine_name VARCHAR(64) NOT NULL COLLATE "
62                          " utf8_general_ci");
63 
64   m_target_def.add_index(INDEX_PK_VIEW_ID_ROUTINE_CATALOG,
65                          "INDEX_PK_VIEW_ID_ROUTINE_CATALOG",
66                          "PRIMARY KEY(view_id, routine_catalog, "
67                          "routine_schema, routine_name)");
68   m_target_def.add_index(INDEX_K_ROUTINE_CATALOG_ROUTINE_SCHEMA_ROUTINE_NAME,
69                          "INDEX_K_ROUTINE_CATALOG_ROUTINE_SCHEMA_ROUTINE_NAME",
70                          "KEY (routine_catalog, routine_schema, "
71                          "routine_name)");
72 
73   m_target_def.add_foreign_key(FK_VIEW_ID, "FK_VIEW_ID",
74                                "FOREIGN KEY (view_id) REFERENCES "
75                                "tables(id)");
76 }
77 
78 ///////////////////////////////////////////////////////////////////////////
79 
create_key_by_view_id(Object_id view_id)80 Object_key *View_routine_usage::create_key_by_view_id(Object_id view_id) {
81   return new (std::nothrow) Parent_id_range_key(
82       INDEX_PK_VIEW_ID_ROUTINE_CATALOG, FIELD_VIEW_ID, view_id);
83 }
84 
85 ///////////////////////////////////////////////////////////////////////////
86 
create_primary_key(Object_id view_id,const String_type & routine_catalog,const String_type & routine_schema,const String_type & routine_name)87 Object_key *View_routine_usage::create_primary_key(
88     Object_id view_id, const String_type &routine_catalog,
89     const String_type &routine_schema, const String_type &routine_name) {
90   return new (std::nothrow) Composite_obj_id_3char_key(
91       INDEX_PK_VIEW_ID_ROUTINE_CATALOG, FIELD_VIEW_ID, view_id,
92       FIELD_ROUTINE_CATALOG, routine_catalog, FIELD_ROUTINE_SCHEMA,
93       routine_schema, FIELD_ROUTINE_NAME, routine_name);
94 }
95 
96 ///////////////////////////////////////////////////////////////////////////
97 
create_key_by_name(const String_type & routine_catalog,const String_type & routine_schema,const String_type & routine_name)98 Object_key *View_routine_usage::create_key_by_name(
99     const String_type &routine_catalog, const String_type &routine_schema,
100     const String_type &routine_name) {
101   return new (std::nothrow) Table_reference_range_key(
102       INDEX_K_ROUTINE_CATALOG_ROUTINE_SCHEMA_ROUTINE_NAME,
103       FIELD_ROUTINE_CATALOG, routine_catalog, FIELD_ROUTINE_SCHEMA,
104       routine_schema, FIELD_ROUTINE_NAME, routine_name);
105 }
106 
107 ///////////////////////////////////////////////////////////////////////////
108 
109 }  // namespace tables
110 }  // namespace dd
111