-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmysql_library_imp.cc
224 lines (197 loc) · 7.58 KB
/
mysql_library_imp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (c) 2024, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "mysql_library_imp.h"
#include <string_view>
#include "sql/current_thd.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/types/library.h"
#include "sql/sp.h"
#include "sql/sp_head.h" // sp_head
constexpr auto MYSQL_SUCCESS = 0;
constexpr auto MYSQL_FAILURE = 1;
static auto acquire_library(THD *thd, std::string_view schema,
std::string_view name) -> const dd::Library * {
if (schema.empty() || name.empty()) return {}; // Error.
// Ensure null-terminated strings.
assert(schema.data()[schema.length()] == '\0');
assert(name.data()[name.length()] == '\0');
auto access_error = check_routine_access(
thd, EXECUTE_ACL, schema.data(), name.data(), Acl_type::LIBRARY, true);
if (access_error) return {};
auto result = static_cast<const dd::Library *>(nullptr);
if (thd->dd_client()->acquire<dd::Library>(schema.data(), name.data(),
&result) == MYSQL_FAILURE)
return {};
return result;
}
class Mysql_library {
THD *m_thd;
bool m_exists{false};
std::string_view m_body;
std::string_view m_language;
MDL_ticket *m_lock{nullptr};
public:
Mysql_library(THD *thd, std::string_view schema,
std::string_view library_name,
[[maybe_unused]] std::string_view version)
: m_thd{thd} {
// Lock the library.
auto mdl_key = MDL_key{};
dd::Library::create_mdl_key(schema.data(), library_name.data(), &mdl_key);
// MDL Lock request on the routine.
auto library_request = MDL_request{};
MDL_REQUEST_INIT_BY_KEY(&library_request, &mdl_key, MDL_SHARED_READ,
MDL_EXPLICIT);
// Acquire an MDL lock.
if (thd->mdl_context.acquire_lock(&library_request,
thd->variables.lock_wait_timeout) ==
MYSQL_FAILURE)
return;
m_lock = library_request.ticket;
if (!m_lock) return;
// Get the Library from the data dictionary.
const auto releaser =
dd::cache::Dictionary_client::Auto_releaser{thd->dd_client()};
const auto library = acquire_library(thd, schema, library_name);
if (!library) return; // The library does not exist.
m_body = library->definition();
m_language = library->external_language();
m_exists = true;
// Existent libraries should be locked.
assert(m_lock);
}
~Mysql_library() {
// The libraries that do not exist, are not locked.
assert(!m_exists || m_lock);
if (m_lock) m_thd->mdl_context.release_lock(m_lock);
m_lock = nullptr;
}
[[nodiscard]] operator bool() const {
if (!m_exists) return false;
assert(m_lock);
if (!m_lock) return false;
if (m_body.empty()) return false;
if (m_language.empty()) return false;
return true;
}
[[nodiscard]] auto get_body() const -> std::string_view {
assert(*this);
return m_body;
}
[[nodiscard]] auto get_language() const -> std::string_view {
assert(*this);
return m_language;
}
};
/// Component service interface implementation.
DEFINE_BOOL_METHOD(mysql_library_imp::exists,
(MYSQL_THD thd, mysql_cstring_with_length schema_name,
mysql_cstring_with_length library_name,
mysql_cstring_with_length version, bool *result)) {
assert(result);
if (!result) return MYSQL_FAILURE;
*result = false;
try {
if (!thd) thd = current_thd;
auto library = Mysql_library{thd,
{schema_name.str, schema_name.length},
{library_name.str, library_name.length},
{version.str, version.length}};
if (!library) return MYSQL_FAILURE;
*result = bool{library};
} catch (...) {
return MYSQL_FAILURE;
}
return MYSQL_SUCCESS;
}
DEFINE_BOOL_METHOD(mysql_library_imp::init,
(MYSQL_THD thd, mysql_cstring_with_length schema_name,
mysql_cstring_with_length library_name,
mysql_cstring_with_length version,
my_h_library *library_handle)) {
assert(library_handle);
if (!library_handle) return MYSQL_FAILURE;
*library_handle = nullptr;
try {
if (!thd) thd = current_thd;
auto library = new (thd->mem_root) Mysql_library(
thd, std::string_view{schema_name.str, schema_name.length},
std::string_view{library_name.str, library_name.length},
std::string_view{version.str, version.length});
if (!library) return MYSQL_FAILURE;
*library_handle = reinterpret_cast<my_h_library>(library);
} catch (...) {
return MYSQL_FAILURE;
}
return MYSQL_SUCCESS;
}
DEFINE_BOOL_METHOD(mysql_library_imp::deinit, (my_h_library library_handle)) {
assert(library_handle);
if (!library_handle) return MYSQL_FAILURE;
try {
auto library = reinterpret_cast<Mysql_library *>(library_handle);
// The Library handle has been allocated in the THD's mem_root
// and will be automatically deallocated. Just destroy the object,
// to release the lock.
library->~Mysql_library();
} catch (...) {
return MYSQL_FAILURE;
}
return MYSQL_SUCCESS;
}
DEFINE_BOOL_METHOD(mysql_library_imp::get_body,
(my_h_library library_handle,
mysql_cstring_with_length *body)) {
assert(body);
if (!body) return MYSQL_FAILURE;
body->str = nullptr;
body->length = 0;
if (!library_handle) return MYSQL_FAILURE;
try {
auto library = reinterpret_cast<Mysql_library *>(library_handle);
if (!*library) return MYSQL_FAILURE; // Non-existent library.
auto library_body = library->get_body();
if (library_body.empty()) return MYSQL_FAILURE;
body->str = library_body.data();
body->length = library_body.length();
} catch (...) {
return MYSQL_FAILURE;
}
return MYSQL_SUCCESS;
}
DEFINE_BOOL_METHOD(mysql_library_imp::get_language,
(my_h_library library_handle,
mysql_cstring_with_length *language)) {
assert(language);
if (!language) return MYSQL_FAILURE;
language->str = nullptr;
language->length = 0;
if (!library_handle) return MYSQL_FAILURE;
try {
auto library = reinterpret_cast<Mysql_library *>(library_handle);
if (!*library) return MYSQL_FAILURE; // Non-existent library.
auto library_language = library->get_language();
if (library_language.empty()) return MYSQL_FAILURE;
language->str = library_language.data();
language->length = library_language.length();
} catch (...) {
return MYSQL_FAILURE;
}
return MYSQL_SUCCESS;
}