MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
routine_utilities.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_
27#define _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_
28
29#include <memory>
30#include <string>
31#include "helper/http/url.h"
33#include "mrs/http/error.h"
37
38namespace mrs {
39namespace endpoint {
40namespace handler {
41
43
44inline std::string get_endpoint_url(
45 std::weak_ptr<mrs::endpoint::DbObjectEndpoint> &wp) {
46 auto locked = lock_or_throw_unavail(wp);
47 return locked->get_url().join();
48}
49
50inline std::string get_path_after_object_name(
51 const ::http::base::Uri &base_uri, const ::http::base::Uri &requests_uri) {
52 const auto &elements_path = requests_uri.get_path_elements();
53 const auto &elements_base = base_uri.get_path_elements();
54
55 if (elements_path.size() > elements_base.size())
56 return elements_path[elements_base.size()];
57
58 return {};
59}
60
61inline std::string get_path_after_object_name(
62 std::weak_ptr<mrs::endpoint::DbObjectEndpoint> &wp,
63 const ::http::base::Uri &requests_uri) {
64 auto endpoint = lock_or_throw_unavail(wp);
65 return get_path_after_object_name(endpoint->get_url(), requests_uri);
66}
67
69
71 using namespace helper;
72 switch (type) {
74 return mysqlrouter::sqlstring("FROM_BASE64(?)");
75
77 return mysqlrouter::sqlstring("ST_GeomFromGeoJSON(?)");
78
79 case DataType::VECTOR:
80 return mysqlrouter::sqlstring("STRING_TO_VECTOR(?)");
81
82 case DataType::JSON:
83 return mysqlrouter::sqlstring("CAST(? as JSON)");
84
85 default: {
86 }
87 }
88
89 return mysqlrouter::sqlstring("?");
90}
91
93 const char *sql_state) {
94 static const std::string k_state_with_user_defined_error = "45000";
95 if (!sql_state) throw e;
96
97 log_debug("While handling a routine, received a mysql-error with state: %s",
98 sql_state);
99 if (k_state_with_user_defined_error != sql_state) {
100 throw e;
101 }
102 // 5000 is the offset for HTTPStatus errors,
103 // Still first HTTP status begins with 100 code,
104 // because of that we are validating the value
105 // not against 5000, but 5100.
106 if (e.code() < 5100 || e.code() >= 5600) {
107 throw e;
108 }
109 helper::json::MapObject map{{"message", e.message()}};
111 try {
113 } catch (...) {
114 throw e;
115 }
116 auto json = helper::json::to_string(map);
117 log_debug("routine - generated custom HTTPstats + message:%s", json.c_str());
118 return HttpResult(status, std::move(json), HttpResult::Type::typeJson);
119}
120
122 bool required) {
123 // this will not be set if the endpoint does not require auth, even if the
124 // request IS authenticated
125 if (!ctxt->user.has_user_id) {
126 if (required) throw http::Error(HttpStatusCode::Forbidden);
127 return {};
128 }
129
130 mysqlrouter::sqlstring sql("?");
131 sql << to_string(ctxt->user.user_id);
132
133 return sql;
134}
135
136} // namespace handler
137} // namespace endpoint
138} // namespace mrs
139
140#endif /* _MYSQL_REST_SERVICE_SRC_MRS_ENDPOINT_HANDLER_ROUTINE_UTILITIES_H_ */
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4618
Definition: error.h:37
Definition: mysql_session.h:288
std::string message() const
Definition: mysql_session.h:303
unsigned int code() const
Definition: mysql_session.h:302
Definition: utils_sqlstring.h:67
Log log_debug(std::cout, "DEBUG")
mrs::rest::Handler::HttpResult HttpResult
Definition: handler_content_file.cc:43
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
auto get_default_status_text(key_type status)
Definition: status_code.h:114
@ GEOMETRY
Definition: mysqlx_resultset.proto:613
@ BINARY
Definition: context.h:38
std::string to_string(const rapidjson::Value &value)
Definition: rapid_json_to_text.h:67
std::map< std::string, std::string > MapObject
std::map that represents simple JSON object.
Definition: to_string.h:44
Definition: cache.h:33
@ typeJson
Definition: media_type.h:47
constexpr key_type Forbidden
Definition: status_code.h:67
Request::Uri Uri
Definition: request.cc:36
ColumnType
Definition: column_type.h:48
mysqlrouter::sqlstring sqlstring
Definition: query_retry_on_ro.cc:36
std::shared_ptr< Type > lock_or_throw_unavail(std::weak_ptr< Type > &endpoint)
Definition: utilities.h:52
std::string get_endpoint_url(std::weak_ptr< mrs::endpoint::DbObjectEndpoint > &wp)
Definition: routine_utilities.h:44
mysqlrouter::sqlstring get_user_id(rest::RequestContext *ctxt, bool required)
Definition: routine_utilities.h:121
mysqlrouter::sqlstring get_sql_format(DataType type)
Definition: routine_utilities.h:70
HandlerAuthorizeAuthApps::HttpResult HttpResult
Definition: handler_authorize_auth_apps.cc:45
mrs::database::entry::ColumnType DataType
Definition: routine_utilities.h:68
HttpResult handler_mysqlerror(const mysqlrouter::MySQLSession::Error &e, const char *sql_state)
Definition: routine_utilities.h:92
std::string get_path_after_object_name(const ::http::base::Uri &base_uri, const ::http::base::Uri &requests_uri)
Definition: routine_utilities.h:50
Definition: authorize_manager.h:48
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2894
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
required string type
Definition: replication_group_member_actions.proto:34
Definition: http_result.h:37
int HttpStatus
Definition: http_result.h:39
Definition: request_context.h:47
AuthUser user
Definition: request_context.h:73