1 /* Copyright (c) 2019, 2020, 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 <mysql/components/component_implementation.h>
24 #include <mysql/components/my_service.h>
25 #include <mysql/components/service.h>
26 #include <mysql/components/service_implementation.h>
27 #include <mysql/components/services/mysql_string.h>
28 #include <mysql/components/services/udf_metadata.h>
29 #include <mysql/components/services/udf_registration.h>
30 #include "udf_extension_test_functions.h"
31
32 REQUIRES_SERVICE_PLACEHOLDER(udf_registration);
33 REQUIRES_SERVICE_PLACEHOLDER(mysql_string_converter);
34 REQUIRES_SERVICE_PLACEHOLDER(mysql_string_factory);
35 REQUIRES_SERVICE_PLACEHOLDER(mysql_udf_metadata);
36
37 /**
38 Plugin init function that registers a UDF.
39 A newly created UDF must be registered here.
40
41 @retval false UDF registered successfully.
42 @retval true Otherwise.
43 */
test_udf_extension_init()44 static int test_udf_extension_init() {
45 bool ret = true;
46 /*
47 Demonstrates how to set and get the charset extension argument of
48 return value. It also demonstrate how to perforn the charset
49 conversion on return value.
50
51 This UDF takes two STRING arguments. It returns the value of first
52 argument. But before returning the value, it converts the return
53 value into the character set of the second argument.
54 */
55 if (mysql_service_udf_registration->udf_register(
56 "test_result_charset", STRING_RESULT,
57 (Udf_func_any)test_result_charset, test_result_charset_init,
58 test_result_charset_deinit)) {
59 goto end;
60 }
61 /*
62 Demonstrates how to set the expected charset of a UDF argument.
63 Users sets the charset of a UDF argument at the init() time, server
64 detects that and provided the converted value at the UDF() time.
65
66 This UDF takes two STRING arguments. It sets the charset of first UDF
67 argument as charset of second argument.
68 */
69 if (mysql_service_udf_registration->udf_register(
70 "test_args_charset", STRING_RESULT, (Udf_func_any)test_args_charset,
71 test_args_charset_init, test_args_charset_deinit)) {
72 goto end;
73 }
74 /*
75 Demonstrates how to set and get the collation extension argument of
76 return value. It also demonstrate how to perforn the charset
77 conversion on return value.
78
79 This UDF takes two STRING arguments. It returns the value of first
80 argument. But before returning the value, it converts the return
81 value into the character set of the second argument. It determines
82 the charset of first argument from the collation name as it was set
83 during init() time.
84 */
85 if (mysql_service_udf_registration->udf_register(
86 "test_result_collation", STRING_RESULT,
87 (Udf_func_any)test_result_collation, test_result_collation_init,
88 test_result_collation_deinit)) {
89 goto end;
90 }
91 /*
92 Demonstrates how to set the expected collation of a UDF argument.
93 Users sets the collation of a UDF argument at the init() time, server
94 detects that and provided the converted value at the UDF() time.
95
96 This UDF takes two STRING arguments. It sets the collation of first UDF
97 argument as collation of second argument.
98 */
99 if (mysql_service_udf_registration->udf_register(
100 "test_args_collation", STRING_RESULT,
101 (Udf_func_any)test_args_collation, test_args_collation_init,
102 test_args_collation_deinit)) {
103 goto end;
104 }
105 /*
106 Demonstrates how to set and get the charset extension argument of
107 return value. It also demonstrate how to perforn the charset conversion
108 on return value.
109
110 This UDF takes two STRING arguments. It returns the value of first
111 argument. But before returning the value, it converts the return
112 value into the character set as it was specified by the user in the second
113 argument.
114 */
115 if (mysql_service_udf_registration->udf_register(
116 "test_result_charset_with_value", STRING_RESULT,
117 (Udf_func_any)test_result_charset_with_value,
118 test_result_charset_with_value_init,
119 test_result_charset_with_value_deinit)) {
120 goto end;
121 }
122 /*
123 Demonstrates how to set the expected charset of a UDF argument.
124 Users sets the charset of a UDF argument at the init() time, server
125 detects that and provided the converted value at the UDF() time.
126
127 This UDF takes two STRING arguments. It sets the charset of first UDF
128 argument as charset provided by the user in the second argument.
129 */
130 if (mysql_service_udf_registration->udf_register(
131 "test_args_charset_with_value", STRING_RESULT,
132 (Udf_func_any)test_args_charset_with_value,
133 test_args_charset_with_value_init,
134 test_args_charset_with_value_deinit)) {
135 goto end;
136 }
137 /*
138 Demonstrates how to set and get the collation extension argument of
139 return value. It also demonstrate how to perforn the charset
140 conversion on return value.
141
142 This UDF takes two STRING arguments. It returns the value of first
143 argument. But before returning the value, it converts the return
144 value into the character set of the second argument. It determines
145 the charset of first argument from the collation name as provided
146 by the user in the second argument.
147 */
148 if (mysql_service_udf_registration->udf_register(
149 "test_result_collation_with_value", STRING_RESULT,
150 (Udf_func_any)test_result_collation_with_value,
151 test_result_collation_with_value_init,
152 test_result_collation_with_value_deinit)) {
153 goto end;
154 }
155 /*
156 Demonstrates how to set the expected collation of a UDF argument.
157 Users sets the collation of a UDF argument at the init() time, server
158 detects that and provided the converted value at the UDF() time.
159
160 This UDF takes two STRING arguments. It sets the collation of first UDF
161 argument as collation provided by the user in the second argument.
162 */
163 if (mysql_service_udf_registration->udf_register(
164 "test_args_collation_with_value", STRING_RESULT,
165 (Udf_func_any)test_args_collation_with_value,
166 test_args_collation_with_value_init,
167 test_args_collation_with_value_deinit)) {
168 goto end;
169 }
170 ret = false; // Successfully initialized the plugin
171 end:
172 return ret ? 1 : 0;
173 }
174
175 /**
176 Plugin deinit function that unregisters a UDF
177
178 @retval false UDF unregistered successfully.
179 @retval true Otherwise.
180 */
test_udf_extension_deinit()181 static int test_udf_extension_deinit() {
182 bool ret = true;
183 int was_present;
184 if (mysql_service_udf_registration->udf_unregister("test_result_charset",
185 &was_present) ||
186 mysql_service_udf_registration->udf_unregister("test_args_charset",
187 &was_present) ||
188 mysql_service_udf_registration->udf_unregister("test_result_collation",
189 &was_present) ||
190 mysql_service_udf_registration->udf_unregister("test_args_collation",
191 &was_present) ||
192 mysql_service_udf_registration->udf_unregister(
193 "test_result_charset_with_value", &was_present) ||
194 mysql_service_udf_registration->udf_unregister(
195 "test_args_charset_with_value", &was_present) ||
196 mysql_service_udf_registration->udf_unregister(
197 "test_result_collation_with_value", &was_present) ||
198 mysql_service_udf_registration->udf_unregister(
199 "test_args_collation_with_value", &was_present)) {
200 goto end;
201 }
202 ret = false;
203 end:
204 return ret ? 1 : 0;
205 }
206
207 BEGIN_COMPONENT_PROVIDES(test_udf_extension)
208 END_COMPONENT_PROVIDES();
209
210 BEGIN_COMPONENT_REQUIRES(test_udf_extension)
211 REQUIRES_SERVICE(udf_registration), REQUIRES_SERVICE(mysql_string_converter),
212 REQUIRES_SERVICE(mysql_string_factory),
213 REQUIRES_SERVICE(mysql_udf_metadata), END_COMPONENT_REQUIRES();
214
215 BEGIN_COMPONENT_METADATA(test_udf_extension)
216 METADATA("mysql.author", "Oracle Corporation"),
217 METADATA("mysql.license", "GPL"), METADATA("test_property", "1"),
218 END_COMPONENT_METADATA();
219
220 DECLARE_COMPONENT(test_udf_extension, "mysql:test_udf_extension")
221 test_udf_extension_init, test_udf_extension_deinit END_DECLARE_COMPONENT();
222
223 DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_udf_extension)
224 END_DECLARE_LIBRARY_COMPONENTS
225